Skip to content

Commit

Permalink
[5.8] change BelongsTo::relation to BelongsTo::relationName(witho…
Browse files Browse the repository at this point in the history
…ut breaking change) (#26724)

* Improved naming convention consistency.

* Renamed the constructor's parameter name too.

* add getRelation back as a deprecated method
  • Loading branch information
yaquawa authored and taylorotwell committed Dec 3, 2018
1 parent 722d02f commit 4fd8a16
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
28 changes: 20 additions & 8 deletions src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class BelongsTo extends Relation
*
* @var string
*/
protected $relation;
protected $relationName;

/**
* The count of self joins.
Expand All @@ -51,13 +51,14 @@ class BelongsTo extends Relation
* @param \Illuminate\Database\Eloquent\Model $child
* @param string $foreignKey
* @param string $ownerKey
* @param string $relation
* @param string $relationName
*
* @return void
*/
public function __construct(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
public function __construct(Builder $query, Model $child, $foreignKey, $ownerKey, $relationName)
{
$this->ownerKey = $ownerKey;
$this->relation = $relation;
$this->relationName = $relationName;
$this->foreignKey = $foreignKey;

// In the underlying base relationship class, this variable is referred to as
Expand Down Expand Up @@ -219,9 +220,9 @@ public function associate($model)
$this->child->setAttribute($this->foreignKey, $ownerKey);

if ($model instanceof Model) {
$this->child->setRelation($this->relation, $model);
$this->child->setRelation($this->relationName, $model);
} elseif ($this->child->isDirty($this->foreignKey)) {
$this->child->unsetRelation($this->relation);
$this->child->unsetRelation($this->relationName);
}

return $this->child;
Expand All @@ -236,7 +237,7 @@ public function dissociate()
{
$this->child->setAttribute($this->foreignKey, null);

return $this->child->setRelation($this->relation, null);
return $this->child->setRelation($this->relationName, null);
}

/**
Expand Down Expand Up @@ -366,8 +367,19 @@ public function getQualifiedOwnerKeyName()
*
* @return string
*/
public function getRelationName()
{
return $this->relationName;
}

/**
* Get the name of the relationship.
*
* @return string
* @deprecated The getRelationName() method should be used instead. Will be removed in Laravel 5.9.
*/
public function getRelation()
{
return $this->relation;
return $this->relationName;
}
}
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ protected function matchToMorphParents($type, Collection $results)

if (isset($this->dictionary[$type][$ownerKey])) {
foreach ($this->dictionary[$type][$ownerKey] as $model) {
$model->setRelation($this->relation, $result);
$model->setRelation($this->relationName, $result);
}
}
}
Expand All @@ -203,7 +203,7 @@ public function associate($model)
$this->morphType, $model instanceof Model ? $model->getMorphClass() : null
);

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

/**
Expand All @@ -217,7 +217,7 @@ public function dissociate()

$this->parent->setAttribute($this->morphType, null);

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

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentBelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function testAssociateMethodSetsForeignKeyOnModelById()
$relation = $this->getRelation($parent);
$parent->shouldReceive('setAttribute')->once()->with('foreign_key', 1);
$parent->shouldReceive('isDirty')->once()->andReturn(true);
$parent->shouldReceive('unsetRelation')->once()->with($relation->getRelation());
$parent->shouldReceive('unsetRelation')->once()->with($relation->getRelationName());
$relation->associate(1);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1090,27 +1090,27 @@ public function testMorphToCreatesProperRelation()
$relation = $model->morphToStub();
$this->assertEquals('morph_to_stub_id', $relation->getForeignKeyName());
$this->assertEquals('morph_to_stub_type', $relation->getMorphType());
$this->assertEquals('morphToStub', $relation->getRelation());
$this->assertEquals('morphToStub', $relation->getRelationName());
$this->assertSame($model, $relation->getParent());
$this->assertInstanceOf(EloquentModelSaveStub::class, $relation->getQuery()->getModel());

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

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

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

public function testBelongsToManyCreatesProperRelation()
Expand Down

0 comments on commit 4fd8a16

Please sign in to comment.