Skip to content

Commit

Permalink
Improve hasOne->withDefault closure option and add array option as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
sileence committed Nov 14, 2016
1 parent b763004 commit d7bfc91
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
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

0 comments on commit d7bfc91

Please sign in to comment.