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.5] Make pivot model instantiable #20179

Merged
merged 3 commits into from
Jul 20, 2017
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
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ public function newCollection(array $models = [])
public function newPivot(Model $parent, array $attributes, $table, $exists, $using = null)
{
return $using ? $using::fromRawAttributes($parent, $attributes, $table, $exists)
: new Pivot($parent, $attributes, $table, $exists);
: (new Pivot())->fromAttributes($parent, $attributes, $table, $exists);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Relations/MorphToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function newPivot(array $attributes = [], $exists = false)
$using = $this->using;

$pivot = $using ? $using::fromRawAttributes($this->parent, $attributes, $this->table, $exists)
: new MorphPivot($this->parent, $attributes, $this->table, $exists);
: (new MorphPivot())->fromAttributes($this->parent, $attributes, $this->table, $exists);

$pivot->setPivotKeys($this->foreignPivotKey, $this->relatedPivotKey)
->setMorphType($this->morphType)
Expand Down
26 changes: 18 additions & 8 deletions src/Illuminate/Database/Eloquent/Relations/Pivot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database\Eloquent\Relations;

use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

Expand Down Expand Up @@ -42,19 +43,17 @@ class Pivot extends Model
* @param array $attributes
* @param string $table
* @param bool $exists
* @return void
* @return $this
*/
public function __construct(Model $parent, $attributes, $table, $exists = false)
public function fromAttributes(Model $parent, $attributes, $table, $exists = false)
{
parent::__construct();

// The pivot model is a "dynamic" model since we will set the tables dynamically
// for the instance. This allows it work for any intermediate tables for the
// many to many relationship that are defined by this developer's classes.
$this->setConnection($parent->getConnectionName())
->setTable($table)
->forceFill($attributes)
->syncOriginal();
->setTable($table)
->forceFill($attributes)
->syncOriginal();

// We store off the parent instance so we will access the timestamp column names
// for the model, since the pivot model timestamps aren't easily configurable
Expand All @@ -64,6 +63,8 @@ public function __construct(Model $parent, $attributes, $table, $exists = false)
$this->exists = $exists;

$this->timestamps = $this->hasTimestampAttributes();

return $this;
}

/**
Expand All @@ -77,7 +78,7 @@ public function __construct(Model $parent, $attributes, $table, $exists = false)
*/
public static function fromRawAttributes(Model $parent, $attributes, $table, $exists = false)
{
$instance = new static($parent, $attributes, $table, $exists);
$instance = (new static())->fromAttributes($parent, $attributes, $table, $exists);

$instance->setRawAttributes($attributes, true);

Expand Down Expand Up @@ -195,4 +196,13 @@ public function getUpdatedAtColumn()
{
return $this->pivotParent->getUpdatedAtColumn();
}

public function getTable()
{
if (! isset($this->table)) {
return str_replace('\\', '', Str::snake(Str::singular(class_basename($this))));
}

return $this->table;
}
}
5 changes: 3 additions & 2 deletions tests/Database/DatabaseEloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,10 @@ public function getRelationArguments()
$related->shouldReceive('getTable')->andReturn('roles');
$related->shouldReceive('getKeyName')->andReturn('id');
$related->shouldReceive('newPivot')->andReturnUsing(function () {
$reflector = new ReflectionClass('Illuminate\Database\Eloquent\Relations\Pivot');
$model = new \Illuminate\Database\Eloquent\Relations\Pivot();
$reflector = new ReflectionClass($model);

return $reflector->newInstanceArgs(func_get_args());
return $reflector->getMethod('fromAttributes')->invokeArgs($model, func_get_args());
});

$builder->shouldReceive('join')->once()->with('user_role', 'roles.id', '=', 'user_role.role_id');
Expand Down
26 changes: 15 additions & 11 deletions tests/Database/DatabaseEloquentPivotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testPropertiesAreSetCorrectly()
$parent->shouldReceive('getConnectionName')->twice()->andReturn('connection');
$parent->getConnection()->getQueryGrammar()->shouldReceive('getDateFormat')->andReturn('Y-m-d H:i:s');
$parent->setDateFormat('Y-m-d H:i:s');
$pivot = new Pivot($parent, ['foo' => 'bar', 'created_at' => '2015-09-12'], 'table', true);
$pivot = (new Pivot())->fromAttributes($parent, ['foo' => 'bar', 'created_at' => '2015-09-12'], 'table', true);

$this->assertEquals(['foo' => 'bar', 'created_at' => '2015-09-12 00:00:00'], $pivot->getAttributes());
$this->assertEquals('connection', $pivot->getConnectionName());
Expand All @@ -32,7 +32,7 @@ public function testMutatorsAreCalledFromConstructor()
$parent = m::mock('Illuminate\Database\Eloquent\Model[getConnectionName]');
$parent->shouldReceive('getConnectionName')->once()->andReturn('connection');

$pivot = new DatabaseEloquentPivotTestMutatorStub($parent, ['foo' => 'bar'], 'table', true);
$pivot = (new DatabaseEloquentPivotTestMutatorStub())->fromAttributes($parent, ['foo' => 'bar'], 'table', true);

$this->assertTrue($pivot->getMutatorCalled());
}
Expand All @@ -51,7 +51,7 @@ public function testPropertiesUnchangedAreNotDirty()
{
$parent = m::mock('Illuminate\Database\Eloquent\Model[getConnectionName]');
$parent->shouldReceive('getConnectionName')->once()->andReturn('connection');
$pivot = new Pivot($parent, ['foo' => 'bar', 'shimy' => 'shake'], 'table', true);
$pivot = (new Pivot())->fromAttributes($parent, ['foo' => 'bar', 'shimy' => 'shake'], 'table', true);

$this->assertEquals([], $pivot->getDirty());
}
Expand All @@ -60,7 +60,7 @@ public function testPropertiesChangedAreDirty()
{
$parent = m::mock('Illuminate\Database\Eloquent\Model[getConnectionName]');
$parent->shouldReceive('getConnectionName')->once()->andReturn('connection');
$pivot = new Pivot($parent, ['foo' => 'bar', 'shimy' => 'shake'], 'table', true);
$pivot = (new Pivot())->fromAttributes($parent, ['foo' => 'bar', 'shimy' => 'shake'], 'table', true);
$pivot->shimy = 'changed';

$this->assertEquals(['shimy' => 'changed'], $pivot->getDirty());
Expand All @@ -71,18 +71,18 @@ public function testTimestampPropertyIsSetIfCreatedAtInAttributes()
$parent = m::mock('Illuminate\Database\Eloquent\Model[getConnectionName,getDates]');
$parent->shouldReceive('getConnectionName')->andReturn('connection');
$parent->shouldReceive('getDates')->andReturn([]);
$pivot = new DatabaseEloquentPivotTestDateStub($parent, ['foo' => 'bar', 'created_at' => 'foo'], 'table');
$pivot = (new DatabaseEloquentPivotTestDateStub())->fromAttributes($parent, ['foo' => 'bar', 'created_at' => 'foo'], 'table');
$this->assertTrue($pivot->timestamps);

$pivot = new DatabaseEloquentPivotTestDateStub($parent, ['foo' => 'bar'], 'table');
$pivot = (new DatabaseEloquentPivotTestDateStub())->fromAttributes($parent, ['foo' => 'bar'], 'table');
$this->assertFalse($pivot->timestamps);
}

public function testKeysCanBeSetProperly()
{
$parent = m::mock('Illuminate\Database\Eloquent\Model[getConnectionName]');
$parent->shouldReceive('getConnectionName')->once()->andReturn('connection');
$pivot = new Pivot($parent, ['foo' => 'bar'], 'table');
$pivot = (new Pivot())->fromAttributes($parent, ['foo' => 'bar'], 'table');
$pivot->setPivotKeys('foreign', 'other');

$this->assertEquals('foreign', $pivot->getForeignKey());
Expand All @@ -91,10 +91,7 @@ public function testKeysCanBeSetProperly()

public function testDeleteMethodDeletesModelByKeys()
{
$parent = m::mock('Illuminate\Database\Eloquent\Model[getConnectionName]');
$parent->guard([]);
$parent->shouldReceive('getConnectionName')->once()->andReturn('connection');
$pivot = $this->getMockBuilder('Illuminate\Database\Eloquent\Relations\Pivot')->setMethods(['newQuery'])->setConstructorArgs([$parent, ['foo' => 'bar'], 'table'])->getMock();
$pivot = $this->getMockBuilder('Illuminate\Database\Eloquent\Relations\Pivot')->setMethods(['newQuery'])->getMock();
$pivot->setPivotKeys('foreign', 'other');
$pivot->foreign = 'foreign.value';
$pivot->other = 'other.value';
Expand All @@ -105,6 +102,13 @@ public function testDeleteMethodDeletesModelByKeys()

$this->assertTrue($pivot->delete());
}

public function testPivotModelTableNameIsSingular()
{
$pivot = new Pivot();

$this->assertEquals('pivot', $pivot->getTable());
}
}

class DatabaseEloquentPivotTestDateStub extends \Illuminate\Database\Eloquent\Relations\Pivot
Expand Down