Skip to content
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

The associate function of morphTo relation get wrong owner key #37054

Closed
locnguyen1842 opened this issue Apr 20, 2021 · 4 comments
Closed

The associate function of morphTo relation get wrong owner key #37054

locnguyen1842 opened this issue Apr 20, 2021 · 4 comments
Labels

Comments

@locnguyen1842
Copy link

locnguyen1842 commented Apr 20, 2021

  • Laravel Version: 8.1.19
  • PHP Version: 7.4.16
  • Database Driver & Version: MySQL

Description:

When using the associate function of morphTo relation, the ownerKey did not get the model key name passed to the associate function. Currently, it's got the previous ownerKey.

Steps To Reproduce:

my_requests (table)

my_request_id created_by created_by_type
1 1 App\Models\Admin

MyRequestModel

protected $primaryKey = 'my_request_id';

public function createdBy()
{
    return $this->morphTo(null, null, 'created_by', null);
}

AdminModel

protected $primaryKey = 'agent_id';

public function createdMyRequests()
{
    return $this->morphMany(MyRequestModel::class, 'createdBy');
}

TenantModel

protected $primaryKey = 'tenant_id';

public function createdMyRequests()
{
    return $this->morphMany(MyRequestModel::class, 'createdBy');
}

MyRequestController

$modifier = TenantModel::find(1);

$myRequest = MyRequestModel::find(1);

$myRequest->createdBy()->associate($modifier);

When I'm dumping the $myRequest, the created_by attribute is null but the created_by_type was changed, so i was checked the associate function on Illuminate\Database\Eloquent\Relations\MorphTo

public function associate($model)
{
        $this->parent->setAttribute(
             $this->foreignKey, $model instanceof Model ? $model->{$this->ownerKey ?: $model->getKeyName()} : null
        );

        $this->parent->setAttribute(
            $this->morphType, $model instanceof Model ? $model->getMorphClass() : null
        );

        return $this->parent->setRelation($this->relationName, $model);
}

At the line $this->foreignKey, $model instanceof Model ? $model->{$this->ownerKey ?: $model->getKeyName()} the property $this->ownerKey currently return the previous ownerKey (admin_id) not the model passed to the function (tenant_id)

@driesvints
Copy link
Member

Heya, thanks for reporting.

I'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new laravel-issue-37054 --github="--public"

After you've posted the repository, I'll try to reproduce the issue.

Thanks!

@locnguyen1842
Copy link
Author

Heya, thanks for reporting.

I'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new laravel-issue-37054 --github="--public"

After you've posted the repository, I'll try to reproduce the issue.

Thanks!

Hey, this is the repository of the issue, please take a look:
https://github.com/locnguyen1842/laravel-issue-37054

Thanks !

@driesvints
Copy link
Member

This seems like a legitimate bug to me. I'm not sure how to solve this one though. Adjusting the associate method is an option maybe but it could imply a breaking change.

@taylorotwell
Copy link
Member

taylorotwell commented Apr 22, 2021

Duplicate of #36644

This occurs when using different primary key conventions for two models that share the same polymorphic child.

Use the disassociate method before associating with an entirely new model that uses a different naming convention for its primary key:

$myRequest->createdBy()->disassociate();
$myRequest->createdBy()->associate($foo);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants