8000 [5.8] `BelongsTo` method name consistency refactoring (#26374) · laravel/framework@2ee1892 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ee1892

Browse files
yaquawataylorotwell
authored andcommitted
[5.8] BelongsTo method name consistency refactoring (#26374)
* Add `getChild` public method. * add `Name` as the suffix.
1 parent 4195a64 commit 2ee1892

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/Illuminate/Database/Eloquent/Relations/BelongsTo.php

+15-5
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery,
253253
}
254254

255255
return $query->select($columns)->whereColumn(
256-
$this->getQualifiedForeignKey(), '=', $query->qualifyColumn($this->ownerKey)
256+
$this->getQualifiedForeignKeyName(), '=', $query->qualifyColumn($this->ownerKey)
257257
);
258258
}
259259

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

276276
return $query->whereColumn(
277-
$hash.'.'.$this->ownerKey, '=', $this->getQualifiedForeignKey()
277+
$hash.'.'.$this->ownerKey, '=', $this->getQualifiedForeignKeyName()
278278
);
279279
}
280280

@@ -310,12 +310,22 @@ protected function newRelatedInstanceFor(Model $parent)
310310
return $this->related->newInstance();
311311
}
312312

313+
/**
314+
* Get the child of the relationship.
315+
*
316+
* @return \Illuminate\Database\Eloquent\Model
317+
*/
318+
public function getChild()
319+
{
320+
return $this->child;
321+
}
322+
313323
/**
314324
* Get the foreign key of the relationship.
315325
*
316326
* @return string
317327
*/
318-
public function getForeignKey()
328+
public function getForeignKeyName()
319329
{
320330
return $this->foreignKey;
321331
}
@@ -325,7 +335,7 @@ public function getForeignKey()
325335
*
326336
* @return string
327337
*/
328-
public function getQualifiedForeignKey()
338+
public function getQualifiedForeignKeyName()
329339
{
330340
return $this->child->qualifyColumn($this->foreignKey);
331341
}
@@ -335,7 +345,7 @@ public function getQualifiedForeignKey()
335345
*
336346
* @return string
8000
337347
*/
338-
public function getOwnerKey()
348+
public function getOwnerKeyName()
339349
{
340350
return $this->ownerKey;
341351
}

tests/Database/DatabaseEloquentModelTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -1071,14 +1071,14 @@ public function testBelongsToCreatesProperRelation()
10711071
$model = new EloquentModelStub;
10721072
$this->addMockConnection($model);
10731073
$relation = $model->belongsToStub();
1074-
$this->assertEquals('belongs_to_stub_id', $relation->getForeignKey());
1074+
$this->assertEquals('belongs_to_stub_id', $relation->getForeignKeyName());
10751075
$this->assertSame($model, $relation->getParent());
10761076
$this->assertInstanceOf(EloquentModelSaveStub::class, $relation->getQuery()->getModel());
10771077

10781078
$model = new EloquentModelStub;
10791079
$this->addMockConnection($model);
10801080
$relation = $model->belongsToExplicitKeyStub();
1081-
$this->assertEquals('foo', $relation->getForeignKey());
1081+
$this->assertEquals('foo', $relation->getForeignKeyName());
10821082
}
10831083

10841084
public function testMorphToCreatesProperRelation()
@@ -1088,27 +1088,27 @@ public function testMorphToCreatesProperRelation()
10881088

10891089
// $this->morphTo();
10901090
$relation = $model->morphToStub();
1091-
$this->assertEquals('morph_to_stub_id', $relation->getForeignKey());
1091+
$this->assertEquals('morph_to_stub_id', $relation->getForeignKeyName());
10921092
$this->assertEquals('morph_to_stub_type', $relation->getMorphType());
10931093
$this->assertEquals('morphToStub', $relation->getRelation());
10941094
$this->assertSame($model, $relation->getParent());
10951095
$this->assertInstanceOf(EloquentModelSaveStub::class, $relation->getQuery()->getModel());
10961096

10971097
// $this->morphTo(null, 'type', 'id');
10981098
$relation2 = $model->morphToStubWithKeys();
1099-
$this->assertEquals('id', $relation2->getForeignKey());
1099+
$this->assertEquals('id', $relation2->getForeignKeyName());
11001100
$this->assertEquals('type', $relation2->getMorphType());
11011101
$this->assertEquals('morphToStubWithKeys', $relation2->getRelation());
11021102

11031103
// $this->morphTo('someName');
11041104
$relation3 = $model->morphToStubWithName();
1105-
$this->assertEquals('some_name_id', $relation3->getForeignKey());
1105+
$this->assertEquals('some_name_id', $relation3->getForeignKeyName());
11061106
$this->assertEquals('some_name_type', $relation3->getMorphType());
11071107
$this->assertEquals('someName', $relation3->getRelation());
11081108

11091109
// $this->morphTo('someName', 'type', 'id');
11101110
$relation4 = $model->morphToStubWithNameAndKeys();
1111-
$this->assertEquals('id', $relation4->getForeignKey());
1111+
$this->assertEquals('id', $relation4->getForeignKeyName());
11121112
$this->assertEquals('type', $relation4->getMorphType());
11131113
$this->assertEquals('someName', $relation4->getRelation());
11141114
}

0 commit comments

Comments
 (0)
0