Skip to content

Commit

Permalink
[5.8] BelongsTo method name consistency refactoring (#26374)
Browse files Browse the repository at this point in the history
* Add `getChild` public method.

* add `Name` as the suffix.
  • Loading branch information
yaquawa authored and taylorotwell committed Nov 6, 2018
1 parent 4195a64 commit 2ee1892
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
20 changes: 15 additions & 5 deletions src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery,
}

return $query->select($columns)->whereColumn(
$this->getQualifiedForeignKey(), '=', $query->qualifyColumn($this->ownerKey)
$this->getQualifiedForeignKeyName(), '=', $query->qualifyColumn($this->ownerKey)
);
}

Expand All @@ -274,7 +274,7 @@ public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder
$query->getModel()->setTable($hash);

return $query->whereColumn(
$hash.'.'.$this->ownerKey, '=', $this->getQualifiedForeignKey()
$hash.'.'.$this->ownerKey, '=', $this->getQualifiedForeignKeyName()
);
}

Expand Down Expand Up @@ -310,12 +310,22 @@ protected function newRelatedInstanceFor(Model $parent)
return $this->related->newInstance();
}

/**
* Get the child of the relationship.
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function getChild()
{
return $this->child;
}

/**
* Get the foreign key of the relationship.
*
* @return string
*/
public function getForeignKey()
public function getForeignKeyName()
{
return $this->foreignKey;
}
Expand All @@ -325,7 +335,7 @@ public function getForeignKey()
*
* @return string
*/
public function getQualifiedForeignKey()
public function getQualifiedForeignKeyName()
{
return $this->child->qualifyColumn($this->foreignKey);
}
Expand All @@ -335,7 +345,7 @@ public function getQualifiedForeignKey()
*
* @return string
*/
public function getOwnerKey()
public function getOwnerKeyName()
{
return $this->ownerKey;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1071,14 +1071,14 @@ public function testBelongsToCreatesProperRelation()
$model = new EloquentModelStub;
$this->addMockConnection($model);
$relation = $model->belongsToStub();
$this->assertEquals('belongs_to_stub_id', $relation->getForeignKey());
$this->assertEquals('belongs_to_stub_id', $relation->getForeignKeyName());
$this->assertSame($model, $relation->getParent());
$this->assertInstanceOf(EloquentModelSaveStub::class, $relation->getQuery()->getModel());

$model = new EloquentModelStub;
$this->addMockConnection($model);
$relation = $model->belongsToExplicitKeyStub();
$this->assertEquals('foo', $relation->getForeignKey());
$this->assertEquals('foo', $relation->getForeignKeyName());
}

public function testMorphToCreatesProperRelation()
Expand All @@ -1088,27 +1088,27 @@ public function testMorphToCreatesProperRelation()

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

// $this->morphTo(null, 'type', 'id');
$relation2 = $model->morphToStubWithKeys();
$this->assertEquals('id', $relation2->getForeignKey());
$this->assertEquals('id', $relation2->getForeignKeyName());
$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_id', $relation3->getForeignKeyName());
$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('id', $relation4->getForeignKeyName());
$this->assertEquals('type', $relation4->getMorphType());
$this->assertEquals('someName', $relation4->getRelation());
}
Expand Down

0 comments on commit 2ee1892

Please sign in to comment.