Skip to content

Commit

Permalink
fix #10501 by fixing morph to naming (#15334)
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid authored and taylorotwell committed Sep 8, 2016
1 parent bb7a505 commit 91d25ee
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
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

0 comments on commit 91d25ee

Please sign in to comment.