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

[5.3] Fix morphTo naming inconsistency #15334

Merged
merged 1 commit into from
Sep 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,10 +805,10 @@ public function morphTo($name = null, $type = null, $id = null)
if (is_null($name)) {
list($current, $caller) = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);

$name = Str::snake($caller['function']);
$name = $caller['function'];
}

list($type, $id) = $this->getMorphs($name, $type, $id);
list($type, $id) = $this->getMorphs(Str::snake($name), $type, $id);

// If the type value is null it is probably safe to assume we're eager loading
// the relationship. In this case we'll just pass in a dummy query where we
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,16 @@ public function getOtherKey()
return $this->otherKey;
}

/**
* Get the name of the relationship.
*
* @return string
*/
public function getRelation()
{
return $this->relation;
}

/**
* Get the fully qualified associated key of the relationship.
*
Expand Down
37 changes: 37 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -956,10 +956,32 @@ public function testMorphToCreatesProperRelation()
{
$model = new EloquentModelStub;
$this->addMockConnection($model);

// $this->morphTo();
$relation = $model->morphToStub();
$this->assertEquals('morph_to_stub_id', $relation->getForeignKey());
$this->assertEquals('morph_to_stub_type', $relation->getMorphType());
$this->assertEquals('morphToStub', $relation->getRelation());
$this->assertSame($model, $relation->getParent());
$this->assertInstanceOf('EloquentModelSaveStub', $relation->getQuery()->getModel());

// $this->morphTo(null, 'type', 'id');
$relation2 = $model->morphToStubWithKeys();
$this->assertEquals('id', $relation2->getForeignKey());
$this->assertEquals('type', $relation2->getMorphType());
$this->assertEquals('morphToStubWithKeys', $relation2->getRelation());

// $this->morphTo('someName');
$relation3 = $model->morphToStubWithName();
$this->assertEquals('some_name_id', $relation3->getForeignKey());
$this->assertEquals('some_name_type', $relation3->getMorphType());
$this->assertEquals('someName', $relation3->getRelation());

// $this->morphTo('someName', 'type', 'id');
$relation4 = $model->morphToStubWithNameAndKeys();
$this->assertEquals('id', $relation4->getForeignKey());
$this->assertEquals('type', $relation4->getMorphType());
$this->assertEquals('someName', $relation4->getRelation());
}

public function testBelongsToManyCreatesProperRelation()
Expand Down Expand Up @@ -1489,6 +1511,21 @@ public function morphToStub()
return $this->morphTo();
}

public function morphToStubWithKeys()
{
return $this->morphTo(null, 'type', 'id');
}

public function morphToStubWithName()
{
return $this->morphTo('someName');
}

public function morphToStubWithNameAndKeys()
{
return $this->morphTo('someName', 'type', 'id');
}

public function belongsToExplicitKeyStub()
{
return $this->belongsTo('EloquentModelSaveStub', 'foo');
Expand Down