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

Fix associate on MorphTo relation #4249

Merged

Conversation

jarektkaczyk
Copy link
Contributor

This fix overrides associate method and allows you to use the method on morphTo relation fixing both issues described below.

Issue 1: Currently morphTo uses associate inherited from belongsTo which is not working at all for non-existing model:

$tag = new Tag;
$tag->taggable()->associate(Post::first());
// Fatal error - loop because of 'null' in this code on Model.php:

// If the type value is null it is probably safe to assume we're eager loading
// the relationship. When that is the case we will pass in a dummy query as
// there are multiple types in the morph and we can't use single queries.
if (is_null($class = $this->$type))
{
    return new MorphTo(
        $this->newQuery(), $this, $id, null, $type, $name
    );
}

Issue 2: Using associate on existing model doesn't update morph_type value:

$tag = new Tag(['body'=>'whatever']);
Post::first()->tags()->save($tag);

$tag->taggable_id; // 1
$tag->taggable_type; //  'Post'

$tag->taggable()->associate(Category::find(5));

$tag->taggable_id; // 5
$tag->taggable_type; // 'Post'     *no update here*

taylorotwell added a commit that referenced this pull request May 2, 2014
@taylorotwell taylorotwell merged commit 7346eb9 into laravel:4.1 May 2, 2014
@jarektkaczyk jarektkaczyk deleted the 4.1-fix-associate-on-morphTo branch July 18, 2014 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants