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.6] Default attributes for pivot table during fetching and creating rows #22867

Merged
merged 7 commits into from
Jan 23, 2018
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
33 changes: 33 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class BelongsToMany extends Relation
*/
protected $pivotWhereIns = [];

/**
* Default values for the pivot columns.
*
* @var array
*/
protected $pivotValues = [];

/**
* Indicates if timestamps are available on the pivot table.
*
Expand Down Expand Up @@ -347,6 +354,32 @@ public function orWherePivot($column, $operator = null, $value = null)
return $this->wherePivot($column, $operator, $value, 'or');
}

/**
* Sets default value when querying or creating a new row in the pivot table.
*
* @param string $column
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function withPivotValues($column, $value = null)
{
if (is_array($column)) {
foreach ($column as $name => $value) {
$this->withPivotValues($name, $value);
}

return $this;
}

if (is_null($value)) {
throw new \InvalidArgumentException('$value cannot be null.');
}

$this->pivotValues[] = func_get_args();

return $this->wherePivot($column, '=', $value);
}

/**
* Set an "or where in" clause for a pivot table column.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ protected function baseAttachRecord($id, $timed)
$record = $this->addTimestampsToAttachment($record);
}

// Adding the default pivot values (if there is any) to the record.
foreach ($this->pivotValues as $arguments) {
list($name, $value) = $arguments;
$record[$name] = $value;
}

return $record;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Illuminate\Tests\Database;

use Mockery as m;
use PHPUnit\Framework\TestCase;

class DatabaseEloquentBelongsToManyWithDefaultAttributesTest extends TestCase
{
public function tearDown()
{
m::close();
}

public function testWithPivotValuesMethodSetsWhereConditionsForFetching()
{
$relation = $this->getMockBuilder('Illuminate\Database\Eloquent\Relations\BelongsToMany')->setMethods(['touchIfTouching'])->setConstructorArgs($this->getRelationArguments())->getMock();
$relation->withPivotValues(['is_admin' => 1]);
}

public function testWithPivotValuesMethodSetsDefaultArgumentsForInsertion()
{
$relation = $this->getMockBuilder('Illuminate\Database\Eloquent\Relations\BelongsToMany')->setMethods(['touchIfTouching'])->setConstructorArgs($this->getRelationArguments())->getMock();
$relation->withPivotValues(['is_admin' => 1]);

$query = m::mock('stdClass');
$query->shouldReceive('from')->once()->with('club_user')->andReturn($query);
$query->shouldReceive('insert')->once()->with([['club_id' => 1, 'user_id' => 1, 'is_admin' => 1]])->andReturn(true);
$relation->getQuery()->shouldReceive('getQuery')->andReturn($mockQueryBuilder = m::mock('stdClass'));
$mockQueryBuilder->shouldReceive('newQuery')->once()->andReturn($query);

$relation->attach(1);
}

public function getRelationArguments()
{
$parent = m::mock('Illuminate\Database\Eloquent\Model');
$parent->shouldReceive('getKey')->andReturn(1);
$parent->shouldReceive('getCreatedAtColumn')->andReturn('created_at');
$parent->shouldReceive('getUpdatedAtColumn')->andReturn('updated_at');
$parent->shouldReceive('getAttribute')->with('id')->andReturn(1);

$builder = m::mock('Illuminate\Database\Eloquent\Builder');
$related = m::mock('Illuminate\Database\Eloquent\Model');
$builder->shouldReceive('getModel')->andReturn($related);

$related->shouldReceive('getTable')->andReturn('users');
$related->shouldReceive('getKeyName')->andReturn('id');

$builder->shouldReceive('join')->once()->with('club_user', 'users.id', '=', 'club_user.user_id');
$builder->shouldReceive('where')->once()->with('club_user.club_id', '=', 1);
$builder->shouldReceive('where')->once()->with('club_user.is_admin', '=', 1, 'and');

return [$builder, $parent, 'club_user', 'club_id', 'user_id', 'id', 'id', null, false];
}
}