You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Assume the most usual One-to-Many relationship (boilerplate code in the collapsible section at the bottom):
Author ◆— Post
When replicating an entity at the "HasMany" side of a relationship (e.g. Author) with Model::replicate(), the resulting entity's dynamic properties may still mistakenly refer to the original entity's relations.
The problem appears if the original entity's relations were loaded before replication.
$author = new \App\Models\Author();
$author->save();
$post = new \App\Models\Post();
$post->author()->associate($author);
$post->save();
// $author has one Post/** * NOT loading the relationship before replication */
dump('NOT loading the relationship before replication');
$author1 = $author->replicate();
dump($author1->posts->count()); // 0 (ok)$author1->save();
dump($author1->posts->count()); // 0 (ok)
dump($author1->posts()->get()->count()); // 0 (ok)/** * Loading relationship */
dump('Loading relationship');
dump($author->posts->count()); // 1 (ok)$author2 = $author->replicate();
dump($author2->posts->count()); // 1 (expected: 0)
dump($author2->posts()->get()->count()); // 0$author2->save();
dump($author2->posts->count()); // 1 (expected: 0)
dump($author2->posts()->get()->count()); // 0 (ok)$author2->refresh();
dump($author2->posts->count()); // 0 (ok)/** * Replicating the One-to- side of the relationship */
dump('Replicating the One-to- side of the relationship');
dump($post->author !== null); // true (ok)$post2 = $post->replicate();
$post2->save();
$post2->refresh();
dump($post2->author !== null); // true (ok)
The same behviour is visible when replicating either side of a Many-to-Many relationship.
Expected result:
the replicated entity's dynamic properties which represent a to-Many relationship are reset
the documentation should make it clear that only -to-One relations are replicated; in particular, Many-to-Many relationships are NOT replicated (where replicating relations could make sense)
This discussion was converted from issue #38901 on September 27, 2021 15:54.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Assume the most usual One-to-Many relationship (boilerplate code in the collapsible section at the bottom):
Author ◆— Post
When replicating an entity at the "HasMany" side of a relationship (e.g. Author) with
Model::replicate()
, the resulting entity's dynamic properties may still mistakenly refer to the original entity's relations.The problem appears if the original entity's relations were loaded before replication.
The same behviour is visible when replicating either side of a Many-to-Many relationship.
Expected result:
System informations:
Laravel: 8.61.0
PHP: v8.0.10
Models and Migrations
Beta Was this translation helpful? Give feedback.
All reactions