Skip to content

Commit

Permalink
allow BelongsTo relations to return defaults
Browse files Browse the repository at this point in the history
this implementation was copied and tweaked from the `HasOne`
relationship.
  • Loading branch information
browner12 committed Jun 22, 2017
1 parent 43bda84 commit 80e7ba4
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 12 deletions.
53 changes: 51 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@

class BelongsTo extends Relation
{
/**
* Indicates if a default model instance should be used.
*
* Alternatively, may be a Closure or array.
*
* @var \Closure|array|bool
*/
protected $withDefault;

/**
* The child model instance of the relation.
*/
Expand Down Expand Up @@ -72,7 +81,7 @@ public function __construct(Builder $query, Model $child, $foreignKey, $ownerKey
*/
public function getResults()
{
return $this->query->first();
return $this->query->first() ?: $this->getDefaultFor($this->parent);
}

/**
Expand Down Expand Up @@ -149,12 +158,39 @@ protected function getEagerModelKeys(array $models)
public function initRelation(array $models, $relation)
{
foreach ($models as $model) {
$model->setRelation($relation, null);
$model->setRelation($relation, $this->getDefaultFor($model));
}

return $models;
}

/**
* Get the default value for this relation.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model|null
*/
protected function getDefaultFor(Model $model)
{
//return nothing if the user doesn't say to
if (! $this->withDefault) {
return;
}

//no need to set any attributes on this
$instance = $this->related->newInstance();

if (is_callable($this->withDefault)) {
return call_user_func($this->withDefault, $instance) ?: $instance;
}

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

return $instance;
}

/**
* Match the eagerly loaded results to their parents.
*
Expand Down Expand Up @@ -342,4 +378,17 @@ public function getRelation()
{
return $this->relation;
}

/**
* Return a new model instance in case the relationship does not exist.
*
* @param \Closure|array|bool $callback
* @return $this
*/
public function withDefault($callback = true)
{
$this->withDefault = $callback;

return $this;
}
}
69 changes: 59 additions & 10 deletions tests/Database/DatabaseEloquentBelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,60 @@

class DatabaseEloquentBelongsToTest extends TestCase
{
protected $builder;

protected $related;

public function tearDown()
{
m::close();
}

public function testBelongsToWithDefault()
{
$relation = $this->getRelation()->withDefault(); //belongsTo relationships

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

$newModel = new EloquentBelongsToModelStub(); //ie Blog

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

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

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

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

$newModel = new EloquentBelongsToModelStub();

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

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

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

public function testBelongsToWithArrayDefault()
{
$relation = $this->getRelation()->withDefault(['username' => 'taylor']);

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

$newModel = new EloquentBelongsToModelStub();

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

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

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

public function testUpdateMethodRetrievesModelAndUpdates()
{
$relation = $this->getRelation();
Expand Down Expand Up @@ -126,18 +175,18 @@ public function testDefaultEagerConstraintsWhenNotIncrementing()

protected function getRelation($parent = null, $incrementing = true, $keyType = 'int')
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder');
$builder->shouldReceive('where')->with('relation.id', '=', 'foreign.value');
$related = m::mock('Illuminate\Database\Eloquent\Model');
$related->incrementing = $incrementing;
$related->shouldReceive('getKeyType')->andReturn($keyType);
$related->shouldReceive('getIncrementing')->andReturn($incrementing);
$related->shouldReceive('getKeyName')->andReturn('id');
$related->shouldReceive('getTable')->andReturn('relation');
$builder->shouldReceive('getModel')->andReturn($related);
$this->builder = m::mock('Illuminate\Database\Eloquent\Builder');
$this->builder->shouldReceive('where')->with('relation.id', '=', 'foreign.value');
$this->related = m::mock('Illuminate\Database\Eloquent\Model');
$this->related->incrementing = $incrementing;
$this->related->shouldReceive('getKeyType')->andReturn($keyType);
$this->related->shouldReceive('getIncrementing')->andReturn($incrementing);
$this->related->shouldReceive('getKeyName')->andReturn('id');
$this->related->shouldReceive('getTable')->andReturn('relation');
$this->builder->shouldReceive('getModel')->andReturn($this->related);
$parent = $parent ?: new EloquentBelongsToModelStub;

return new BelongsTo($builder, $parent, 'foreign_key', 'id', 'relation');
return new BelongsTo($this->builder, $parent, 'foreign_key', 'id', 'relation');
}
}

Expand Down

0 comments on commit 80e7ba4

Please sign in to comment.