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.3] Improve the "with default" option in the has one relation. #16382

Merged
merged 1 commit into from
Nov 15, 2016
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
21 changes: 15 additions & 6 deletions src/Illuminate/Database/Eloquent/Relations/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Illuminate\Database\Eloquent\Relations;

use Closure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;

Expand Down Expand Up @@ -77,12 +76,22 @@ public function match(array $models, Collection $results, $relation)
*/
protected function getDefaultFor(Model $model)
{
if (! $this->withDefault) {
return;
}

$instance = $this->related->newInstance()->setAttribute(
$this->getPlainForeignKey(), $model->getAttribute($this->localKey)
);

if (is_callable($this->withDefault)) {
return call_user_func($this->withDefault);
} elseif ($this->withDefault === true) {
return $this->related->newInstance()->setAttribute(
$this->getPlainForeignKey(), $model->getAttribute($this->localKey)
);
return call_user_func($this->withDefault, $instance) ?: $instance;
}

if (is_array($this->withDefault)) {
$instance->forceFill($this->withDefault);
}

return $instance;
}
}
44 changes: 41 additions & 3 deletions tests/Database/DatabaseEloquentHasOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,51 @@ public function testHasOneWithDefault()

$this->builder->shouldReceive('first')->once()->andReturnNull();

$newModel = m::mock('Illuminate\Database\Eloquent\Model');
$newModel = new EloquentHasOneModelStub();

$newModel->shouldReceive('setAttribute')->once()->with('foreign_key', 1)->andReturn($newModel);
$this->related->shouldReceive('newInstance')->once()->andReturn($newModel);

$this->assertSame($newModel, $relation->getResults());

$this->assertSame(1, $newModel->getAttribute('foreign_key'));
}

public function testHasOneWithDynamicDefault()
{
$relation = $this->getRelation()->withDefault(function ($newModel) {
$newModel->username = 'taylor';
});

$this->builder->shouldReceive('first')->once()->andReturnNull();

$newModel = new EloquentHasOneModelStub();

$this->related->shouldReceive('newInstance')->once()->andReturn($newModel);

$this->assertInstanceOf('Illuminate\Database\Eloquent\Model', $relation->getResults());
$this->assertSame($newModel, $relation->getResults());

$this->assertSame('taylor', $newModel->username);

$this->assertSame(1, $newModel->getAttribute('foreign_key'));
}

public function testHasOneWithArrayDefault()
{
$attributes = ['username' => 'taylor'];

$relation = $this->getRelation()->withDefault($attributes);

$this->builder->shouldReceive('first')->once()->andReturnNull();

$newModel = new EloquentHasOneModelStub();

$this->related->shouldReceive('newInstance')->once()->andReturn($newModel);

$this->assertSame($newModel, $relation->getResults());

$this->assertSame('taylor', $newModel->username);

$this->assertSame(1, $newModel->getAttribute('foreign_key'));
}

public function testSaveMethodSetsForeignKeyOnModel()
Expand Down