Last active
June 25, 2023 14:39
-
-
Save DavidKloucek/a0f60f14aea0c450a5c5a7874007950b to your computer and use it in GitHub Desktop.
Laravel Eloquent problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function testIdentityMap() | |
{ | |
Comment::query()->where(['id' => 1])->update(['content' => 'a']); // set default value for the first comment | |
$user = User::query()->find(1); | |
$user->name = 'David '.date('s'); | |
$user->save(); | |
$user2 = User::query()->find(1); | |
dump($user2->name === $user->name); // true | |
$firstComment = Comment::query()->find(1); | |
foreach ($user->comments as $c) { | |
if ($c->id === 1) { | |
dump($c === $firstComment); // false | |
$c->content = 'aaa'; | |
$c->save(); | |
dump($c->content); // "aaa" | |
dump($firstComment->content); // "a" .. wrong value | |
$firstComment->refresh(); // fix it | |
dump($firstComment->content); // "aaa" | |
dump($firstComment->content === $c->content); // finally true! | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment