Skip to content

Commit 7c42890

Browse files
[11.x] Apply relation constraitns on upsert (#52239)
* [11.x] Apply relation constraitns on upsert * tests * Update MorphOneOrMany.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent b6729d9 commit 7c42890

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php

+17
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,23 @@ public function updateOrCreate(array $attributes, array $values = [])
276276
});
277277
}
278278

279+
/**
280+
* Insert new records or update the existing ones.
281+
*
282+
* @param array $values
283+
* @param array|string $uniqueBy
284+
* @param array|null $update
285+
* @return int
286+
*/
287+
public function upsert(array $values, $uniqueBy, $update = null)
288+
{
289+
foreach ($values as $key => $value) {
290+
$values[$key][$this->getForeignKeyName()] = $this->getParentKey();
291+
}
292+
293+
return $this->getQuery()->upsert($values, $uniqueBy, $update);
294+
}
295+
279296
/**
280297
* Attach a model instance to the parent model.
281298
*

src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php

+17
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ protected function setForeignAttributesForCreate(Model $model)
9696
$model->{$this->getMorphType()} = $this->morphClass;
9797
}
9898

99+
/**
100+
* Insert new records or update the existing ones.
101+
*
102+
* @param array $values
103+
* @param array|string $uniqueBy
104+
* @param array|null $update
105+
* @return int
106+
*/
107+
public function upsert(array $values, $uniqueBy, $update = null)
108+
{
109+
foreach ($values as $key => $value) {
110+
$values[$key][$this->getMorphType()] = $this->getMorphClass();
111+
}
112+
113+
return parent::upsert($values, $uniqueBy, $update);
114+
}
115+
99116
/** @inheritDoc */
100117
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
101118
{

tests/Database/DatabaseEloquentHasManyTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,29 @@ public function testUpdateOrCreateMethodCreatesNewModelWithForeignKeySet()
253253
$this->assertInstanceOf(Model::class, $relation->updateOrCreate(['foo'], ['bar']));
254254
}
255255

256+
public function testRelationUpsertFillsForeignKey()
257+
{
258+
$relation = $this->getRelation();
259+
260+
$relation->getQuery()->shouldReceive('upsert')->with(
261+
[
262+
['email' => 'foo3', 'name' => 'bar', $relation->getForeignKeyName() => $relation->getParentKey()],
263+
['name' => 'bar2', 'email' => 'foo2', $relation->getForeignKeyName() => $relation->getParentKey()],
264+
],
265+
['email'],
266+
['name']
267+
);
268+
269+
$relation->upsert(
270+
[
271+
['email' => 'foo3', 'name' => 'bar'],
272+
['name' => 'bar2', 'email' => 'foo2'],
273+
],
274+
['email'],
275+
['name']
276+
);
277+
}
278+
256279
public function testRelationIsProperlyInitialized()
257280
{
258281
$relation = $this->getRelation();

tests/Database/DatabaseEloquentMorphTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,29 @@ public function testMorphManyEagerConstraintsAreProperlyAdded()
6767
$relation->addEagerConstraints([$model1, $model2]);
6868
}
6969

70+
public function testMorphRelationUpsertFillsForeignKey()
71+
{
72+
$relation = $this->getManyRelation();
73+
74+
$relation->getQuery()->shouldReceive('upsert')->with(
75+
[
76+
['email' => 'foo3', 'name' => 'bar', $relation->getForeignKeyName() => $relation->getParentKey(), $relation->getMorphType() => $relation->getMorphClass()],
77+
['name' => 'bar2', 'email' => 'foo2', $relation->getForeignKeyName() => $relation->getParentKey(), $relation->getMorphType() => $relation->getMorphClass()],
78+
],
79+
['email'],
80+
['name']
81+
);
82+
83+
$relation->upsert(
84+
[
85+
['email' => 'foo3', 'name' => 'bar'],
86+
['name' => 'bar2', 'email' => 'foo2'],
87+
],
88+
['email'],
89+
['name']
90+
);
91+
}
92+
7093
public function testMakeFunctionOnMorph()
7194
{
7295
$_SERVER['__eloquent.saved'] = false;

0 commit comments

Comments
 (0)