Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Related model uses parent connection when $connection is null #52734

Closed
lombervid opened this issue Sep 10, 2024 · 0 comments
Closed

Related model uses parent connection when $connection is null #52734

lombervid opened this issue Sep 10, 2024 · 0 comments

Comments

@lombervid
Copy link
Contributor

lombervid commented Sep 10, 2024

Laravel Version

11.19.0

PHP Version

8.3.8

Database Driver & Version

MySQL 5.7.44

Description

Having a model, without $connection property specified, as a relation from another model with explicit $connection causes the child to use the parent's connection instead of the default connection.

I found out this behavior was implemented in #16103 but I think it might not be the desired behavior.
According with what the documentation says, it should use the default connection (and that was exactly what I was expecting):

By default, all Eloquent models will use the default database connection that is configured for your application. If you would like to specify a different connection that should be used when interacting with a particular model, you should define a $connection property on the model:

Proposed solution:

I think it could be implemented something like this:

Model.php

<?php

namespace Illuminate\Database\Eloquent;

abstract class Model
{
    // ...
    protected $modelShouldInheritConnection = false;

    // ...

    public function inheritsConnection(): bool
    {
        return $this->$modelShouldInheritConnection;
    }

    // ...
}

HasRelationships.php

<?php

namespace Illuminate\Database\Eloquent\Concerns;

trait HasRelationships
{
    // ...

    protected function newRelatedInstance($class)
    {
        return tap(new $class, function ($instance) {
            if ($instance->inheritsConnection() && ! $instance->getConnectionName()) {
                $instance->setConnection($this->connection);
            }
        });
    }

    // ...
}

Then in the model you want to inherit the parent's connection:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Child extends Model
{
    protected $modelShouldInheritConnection = true;
}

Or it could be set to true by default so as not to break the current behavior.

Kind regards!

Steps To Reproduce

Child Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Child extends Model
{
    //
}

Parent Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Parent extends Model
{
    protected $connection = 'another';

    public function child(): BelongsTo
    {
        // `Child` model will use `another` connection instead of the default
        return $this->belongsTo(Child::class);
    }
}
@laravel laravel locked and limited conversation to collaborators Sep 11, 2024
@crynobone crynobone converted this issue into discussion #52735 Sep 11, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant