Details
Description
Let's say we have three entities: User, Channel, and Log
Every time a User is created or updated, Channels are assigned to a Many-To-Many collection.
This action gets logged in the Log entity.
I have reproduced the bug in the Doctrine sandbox (see attachment). It's a fully working example.
This is a summary:
This works
// Fetch two channels in an array $channels = ....... // create a new user and assign channels $user = new User(); $user->setName('FooBar'); $user->setChannels($channels); //save the user $em->persist($user); $em->flush(); // log it $log = new Log(); $log->setMessage('User created with two channels'); $log->setItem($user); $em->persist($log); $em->flush();
This also works
// Fetch two channels in an array $channels = ....... // fetch an existing user, *change something to the user*, and assign channels $user = ............. $user->setName('Gaz'); $user->setChannels($channels); //save the user $em->persist($user); $em->flush(); // log it $log = new Log(); $log->setMessage('User created with two channels'); $log->setItem($user); $em->persist($log); $em->flush();
This does not work (channels are removed)
// Fetch two channels in an array $channels = ....... // fetch an existing user and only assign channels, change nothing else $user = ............. $user->setChannels($channels); //save the user $em->persist($user); $em->flush(); // log it $log = new Log(); $log->setMessage('User created with two channels'); $log->setItem($user); $em->persist($log); $em->flush();
This last piece of code generates following SQL code (summarized):
DELETE FROM user_channels WHERE userId = 1 INSERT INTO user_channels (userId, channelId) VALUES (1, 2); DELETE FROM user_channels WHERE userId = 1
If you remove the $em->flush() after $em->persist($user); in the last example, the code works (channels are persisted).
I did not have this problem with Doctrine2 Alpha4.
Issue Links
- is duplicated by
-
DDC-983
Creating new collection for manyToMany results in loss of data with multiple flushes
-
Activity
Thomas G.
made changes -
| Field | Original Value | New Value |
|---|---|---|
| Description |
Let's say we have three entities: User, Channel, and Log Every time a User is created or updated, Channels are assigned to a Many-To-Many collection. This action gets logged in the Log entity. I have reproduced the bug in the Doctrine sandbox (see attachment). It's a fully working example. This is a summary: {code:title=This works} // Fetch two channels in an array $channels = ....... // create a new user and assign channels $user = new User(); $user->setName('FooBar'); $user->setChannels($channels); //save the user $em->persist($user); $em->flush(); // log it $log = new Log(); $log->setMessage('User created with two channels'); $log->setItem($user); $em->persist($log); $em->flush(); {code} {code:title=This also works} // Fetch two channels in an array $channels = ....... // fetch an existing user, *change something to the user*, and assign channels $user = ............. $user->setName('Gaz'); $user->setChannels($channels); //save the user $em->persist($user); $em->flush(); // log it $log = new Log(); $log->setMessage('User created with two channels'); $log->setItem($user); $em->persist($log); $em->flush(); {code} {code:title=This does not work} // Fetch two channels in an array $channels = ....... // fetch an existing user and only assign channels, change nothing else $user = ............. $user->setChannels($channels); //save the user $em->persist($user); $em->flush(); // log it $log = new Log(); $log->setMessage('User created with two channels'); $log->setItem($user); $em->persist($log); $em->flush(); {code} This last piece of code generates following SQL code (summarized): {code} DELETE FROM user_channels WHERE userId = 1 INSERT INTO user_channels (userId, channelId) VALUES (1, 2); DELETE FROM user_channels WHERE userId = 1 {code} If you remove the _$em->flush()_ after _$em->persist($user);_ in the last example, the code works. I did not have this problem with Doctrine2 Alpha4. |
Let's say we have three entities: User, Channel, and Log Every time a User is created or updated, Channels are assigned to a Many-To-Many collection. This action gets logged in the Log entity. I have reproduced the bug in the Doctrine sandbox (see attachment). It's a fully working example. This is a summary: {code:title=This works} // Fetch two channels in an array $channels = ....... // create a new user and assign channels $user = new User(); $user->setName('FooBar'); $user->setChannels($channels); //save the user $em->persist($user); $em->flush(); // log it $log = new Log(); $log->setMessage('User created with two channels'); $log->setItem($user); $em->persist($log); $em->flush(); {code} {code:title=This also works} // Fetch two channels in an array $channels = ....... // fetch an existing user, *change something to the user*, and assign channels $user = ............. $user->setName('Gaz'); $user->setChannels($channels); //save the user $em->persist($user); $em->flush(); // log it $log = new Log(); $log->setMessage('User created with two channels'); $log->setItem($user); $em->persist($log); $em->flush(); {code} {code:title=This does not work (channels are removed)} // Fetch two channels in an array $channels = ....... // fetch an existing user and only assign channels, change nothing else $user = ............. $user->setChannels($channels); //save the user $em->persist($user); $em->flush(); // log it $log = new Log(); $log->setMessage('User created with two channels'); $log->setItem($user); $em->persist($log); $em->flush(); {code} This last piece of code generates following SQL code (summarized): {code} DELETE FROM user_channels WHERE userId = 1 INSERT INTO user_channels (userId, channelId) VALUES (1, 2); DELETE FROM user_channels WHERE userId = 1 {code} If you remove the _$em->flush()_ after _$em->persist($user);_ in the last example, the code works. I did not have this problem with Doctrine2 Alpha4. |
Thomas G.
made changes -
| Description |
Let's say we have three entities: User, Channel, and Log Every time a User is created or updated, Channels are assigned to a Many-To-Many collection. This action gets logged in the Log entity. I have reproduced the bug in the Doctrine sandbox (see attachment). It's a fully working example. This is a summary: {code:title=This works} // Fetch two channels in an array $channels = ....... // create a new user and assign channels $user = new User(); $user->setName('FooBar'); $user->setChannels($channels); //save the user $em->persist($user); $em->flush(); // log it $log = new Log(); $log->setMessage('User created with two channels'); $log->setItem($user); $em->persist($log); $em->flush(); {code} {code:title=This also works} // Fetch two channels in an array $channels = ....... // fetch an existing user, *change something to the user*, and assign channels $user = ............. $user->setName('Gaz'); $user->setChannels($channels); //save the user $em->persist($user); $em->flush(); // log it $log = new Log(); $log->setMessage('User created with two channels'); $log->setItem($user); $em->persist($log); $em->flush(); {code} {code:title=This does not work (channels are removed)} // Fetch two channels in an array $channels = ....... // fetch an existing user and only assign channels, change nothing else $user = ............. $user->setChannels($channels); //save the user $em->persist($user); $em->flush(); // log it $log = new Log(); $log->setMessage('User created with two channels'); $log->setItem($user); $em->persist($log); $em->flush(); {code} This last piece of code generates following SQL code (summarized): {code} DELETE FROM user_channels WHERE userId = 1 INSERT INTO user_channels (userId, channelId) VALUES (1, 2); DELETE FROM user_channels WHERE userId = 1 {code} If you remove the _$em->flush()_ after _$em->persist($user);_ in the last example, the code works. I did not have this problem with Doctrine2 Alpha4. |
Let's say we have three entities: User, Channel, and Log Every time a User is created or updated, Channels are assigned to a Many-To-Many collection. This action gets logged in the Log entity. I have reproduced the bug in the Doctrine sandbox (see attachment). It's a fully working example. This is a summary: {code:title=This works} // Fetch two channels in an array $channels = ....... // create a new user and assign channels $user = new User(); $user->setName('FooBar'); $user->setChannels($channels); //save the user $em->persist($user); $em->flush(); // log it $log = new Log(); $log->setMessage('User created with two channels'); $log->setItem($user); $em->persist($log); $em->flush(); {code} {code:title=This also works} // Fetch two channels in an array $channels = ....... // fetch an existing user, *change something to the user*, and assign channels $user = ............. $user->setName('Gaz'); $user->setChannels($channels); //save the user $em->persist($user); $em->flush(); // log it $log = new Log(); $log->setMessage('User created with two channels'); $log->setItem($user); $em->persist($log); $em->flush(); {code} {code:title=This does not work (channels are removed)} // Fetch two channels in an array $channels = ....... // fetch an existing user and only assign channels, change nothing else $user = ............. $user->setChannels($channels); //save the user $em->persist($user); $em->flush(); // log it $log = new Log(); $log->setMessage('User created with two channels'); $log->setItem($user); $em->persist($log); $em->flush(); {code} This last piece of code generates following SQL code (summarized): {code} DELETE FROM user_channels WHERE userId = 1 INSERT INTO user_channels (userId, channelId) VALUES (1, 2); DELETE FROM user_channels WHERE userId = 1 {code} If you remove the _$em->flush()_ after _$em->persist($user);_ in the last example, the code works (channels are persisted). I did not have this problem with Doctrine2 Alpha4. |
Benjamin Eberlei
made changes -
Benjamin Eberlei
made changes -
| Status | Open [ 1 ] | Resolved [ 5 ] |
| Fix Version/s | 2.0.1 [ 10114 ] | |
| Fix Version/s | 2.1 [ 10022 ] | |
| Resolution | Fixed [ 1 ] |
Benjamin Eberlei
made changes -
| Workflow | jira [ 12292 ] | jira-feedback [ 14726 ] |
Benjamin Eberlei
made changes -
| Workflow | jira-feedback [ 14726 ] | jira-feedback2 [ 16590 ] |
Benjamin Eberlei
made changes -
| Workflow | jira-feedback2 [ 16590 ] | jira-feedback3 [ 18843 ] |