Skip to content

[11.x] Add pending attributes to child models #53677

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

Closed
wants to merge 9 commits into from
Closed
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: 2 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ function ($hasOne) {
if ($inverse = $this->getInverseRelationship()) {
$hasOne->inverse($inverse);
}

$hasOne->pendingAttributes = $this->pendingAttributes;
}
));
}
Expand Down
36 changes: 36 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database\Eloquent\Relations;

use Illuminate\Contracts\Database\Query\Expression;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -34,6 +35,13 @@ abstract class HasOneOrMany extends Relation
*/
protected $localKey;

/**
* Attributes to be added to new instances of related models.
*
* @var array
*/
public $pendingAttributes = [];

/**
* Create a new has one or many relationship instance.
*
Expand Down Expand Up @@ -447,6 +455,12 @@ protected function setForeignAttributesForCreate(Model $model)
{
$model->setAttribute($this->getForeignKeyName(), $this->getParentKey());

foreach ($this->pendingAttributes as $key => $value) {
if (! $model->hasAttribute($key)) {
$model->setAttribute($key, $value);
}
}

$this->applyInverseRelationToModel($model);
}

Expand Down Expand Up @@ -507,6 +521,28 @@ public function limit($value)
return $this;
}

/**
* Add attributes to be added to new instances of related models.
*
* @param array|string|\Illuminate\Contracts\Database\Query\Expression $attributes
* @param mixed $value
* @return $this
*/
public function withAttributes(array|string|Expression $attributes, $value = null)
{
if (! is_array($attributes)) {
$attributes = [$attributes => $value];
}

foreach ($attributes as $column => $value) {
$this->where($column, $value);
}

$this->pendingAttributes = array_merge($this->pendingAttributes, $attributes);

return $this;
}

/**
* Get the key for comparing against the parent key in "has" query.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ function ($morphOne) {
if ($inverse = $this->getInverseRelationship()) {
$morphOne->inverse($inverse);
}

$morphOne->pendingAttributes = $this->pendingAttributes;
}
));
}
Expand Down
6 changes: 6 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ protected function setForeignAttributesForCreate(Model $model)

$model->{$this->getMorphType()} = $this->morphClass;

foreach ($this->pendingAttributes as $key => $value) {
if (! $model->hasAttribute($key)) {
$model->setAttribute($key, $value);
}
}

$this->applyInverseRelationToModel($model);
}

Expand Down
275 changes: 275 additions & 0 deletions tests/Database/DatabaseEloquentHasOneOrManyWithAttributesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
<?php

namespace Illuminate\Tests\Database;

use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model;
use PHPUnit\Framework\TestCase;

class DatabaseEloquentHasOneOrManyWithAttributesTest extends TestCase
{
protected function setUp(): void
{
$db = new DB;

$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->bootEloquent();
$db->setAsGlobal();
}

public function testHasManyAddsAttributes(): void
{
$parentId = 123;
$key = 'a key';
$value = 'the value';

$parent = new RelatedWithAttributesModel;
$parent->id = $parentId;

$relationship = $parent
->hasMany(RelatedWithAttributesModel::class, 'parent_id')
->withAttributes([$key => $value]);

$relatedModel = $relationship->make();

$this->assertSame($parentId, $relatedModel->parent_id);
$this->assertSame($value, $relatedModel->$key);
}

public function testHasOneAddsAttributes(): void
{
$parentId = 123;
$key = 'a key';
$value = 'the value';

$parent = new RelatedWithAttributesModel;
$parent->id = $parentId;

$relationship = $parent
->hasOne(RelatedWithAttributesModel::class, 'parent_id')
->withAttributes([$key => $value]);

$relatedModel = $relationship->make();

$this->assertSame($parentId, $relatedModel->parent_id);
$this->assertSame($value, $relatedModel->$key);
}

public function testMorphManyAddsAttributes(): void
{
$parentId = 123;
$key = 'a key';
$value = 'the value';

$parent = new RelatedWithAttributesModel;
$parent->id = $parentId;

$relationship = $parent
->morphMany(RelatedWithAttributesModel::class, 'relatable')
->withAttributes([$key => $value]);

$relatedModel = $relationship->make();

$this->assertSame($parentId, $relatedModel->relatable_id);
$this->assertSame($parent::class, $relatedModel->relatable_type);
$this->assertSame($value, $relatedModel->$key);
}

public function testMorphOneAddsAttributes(): void
{
$parentId = 123;
$key = 'a key';
$value = 'the value';

$parent = new RelatedWithAttributesModel;
$parent->id = $parentId;

$relationship = $parent
->morphOne(RelatedWithAttributesModel::class, 'relatable')
->withAttributes([$key => $value]);

$relatedModel = $relationship->make();

$this->assertSame($parentId, $relatedModel->relatable_id);
$this->assertSame($parent::class, $relatedModel->relatable_type);
$this->assertSame($value, $relatedModel->$key);
}

public function testWithAttributesCanBeOverriden(): void
{
$key = 'a key';
$defaultValue = 'a value';
$value = 'the value';

$parent = new RelatedWithAttributesModel;

$relationship = $parent
->hasMany(RelatedWithAttributesModel::class, 'relatable')
->withAttributes([$key => $defaultValue]);

$relatedModel = $relationship->make([$key => $value]);

$this->assertSame($value, $relatedModel->$key);
}

public function testQueryingDoesNotBreakWither(): void
{
$parentId = 123;
$key = 'a key';
$value = 'the value';

$parent = new RelatedWithAttributesModel;
$parent->id = $parentId;

$relationship = $parent
->hasMany(RelatedWithAttributesModel::class, 'parent_id')
->where($key, $value)
->withAttributes([$key => $value]);

$relatedModel = $relationship->make();

$this->assertSame($parentId, $relatedModel->parent_id);
$this->assertSame($value, $relatedModel->$key);
}

public function testAttributesCanBeAppended(): void
{
$parent = new RelatedWithAttributesModel;

$relationship = $parent
->hasMany(RelatedWithAttributesModel::class, 'parent_id')
->withAttributes(['a' => 'A'])
->withAttributes(['b' => 'B'])
->withAttributes(['a' => 'AA']);

$relatedModel = $relationship->make([
'b' => 'BB',
'c' => 'C',
]);

$this->assertSame('AA', $relatedModel->a);
$this->assertSame('BB', $relatedModel->b);
$this->assertSame('C', $relatedModel->c);
}

public function testSingleAttributeApi(): void
{
$parent = new RelatedWithAttributesModel;
$key = 'attr';
$value = 'Value';

$relationship = $parent
->hasMany(RelatedWithAttributesModel::class, 'parent_id')
->withAttributes($key, $value);

$relatedModel = $relationship->make();

$this->assertSame($value, $relatedModel->$key);
}

public function testWheresAreSet(): void
{
$parentId = 123;
$key = 'a key';
$value = 'the value';

$parent = new RelatedWithAttributesModel;
$parent->id = $parentId;

$relationship = $parent
->hasMany(RelatedWithAttributesModel::class, 'parent_id')
->withAttributes([$key => $value]);

$wheres = $relationship->toBase()->wheres;

$this->assertContains([
'type' => 'Basic',
'column' => $key,
'operator' => '=',
'value' => $value,
'boolean' => 'and',
], $wheres);

// Ensure this doesn't break the default where either.
$this->assertContains([
'type' => 'Basic',
'column' => $parent->qualifyColumn('parent_id'),
'operator' => '=',
'value' => $parentId,
'boolean' => 'and',
], $wheres);
}

public function testNullValueIsAccepted(): void
{
$parentId = 123;
$key = 'a key';

$parent = new RelatedWithAttributesModel;
$parent->id = $parentId;

$relationship = $parent
->hasMany(RelatedWithAttributesModel::class, 'parent_id')
->withAttributes([$key => null]);

$wheres = $relationship->toBase()->wheres;
$relatedModel = $relationship->make();

$this->assertNull($relatedModel->$key);

$this->assertContains([
'type' => 'Null',
'column' => $key,
'boolean' => 'and',
], $wheres);
}

public function testOneKeepsAttributesFromHasMany(): void
{
$parentId = 123;
$key = 'a key';
$value = 'the value';

$parent = new RelatedWithAttributesModel;
$parent->id = $parentId;

$relationship = $parent
->hasMany(RelatedWithAttributesModel::class, 'parent_id')
->withAttributes([$key => $value])
->one();

$relatedModel = $relationship->make();

$this->assertSame($parentId, $relatedModel->parent_id);
$this->assertSame($value, $relatedModel->$key);
}

public function testOneKeepsAttributesFromMorphMany(): void
{
$parentId = 123;
$key = 'a key';
$value = 'the value';

$parent = new RelatedWithAttributesModel;
$parent->id = $parentId;

$relationship = $parent
->morphMany(RelatedWithAttributesModel::class, 'relatable')
->withAttributes([$key => $value])
->one();

$relatedModel = $relationship->make();

$this->assertSame($parentId, $relatedModel->relatable_id);
$this->assertSame($parent::class, $relatedModel->relatable_type);
$this->assertSame($value, $relatedModel->$key);
}
}

class RelatedWithAttributesModel extends Model
{
protected $guarded = [];
}