Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Blocker
-
Resolution: Fixed
-
Affects Version/s: 1.0.12, 1.1.4, 1.2.0-ALPHA1
-
Fix Version/s: 1.2.0-BETA1
-
Component/s: Relations
-
Labels:None
-
Environment:tested on
PHP Version 5.2.6-3ubuntu4.2, PHP Version 5.3
MySQL, PgSQL
Doctrine Version 1.0.11 Revision: 6380
Doctrine Version 1.1.3 Revision: 6380
Doctrine Version 1.1.4 Revision: 6409
Doctrine Version 1.2
Description
Linking between one to many relations with synchronizeWithArray() has a strange behavior.
Example (fully example is in the attachment):
Group has many Users
User has one Group
When I have a group-object (id 2) and do the following:
$group->synchronizeWithArray(array( 'Users' => array(1,2) )); $group->save();
on the first execute of the script the users with id 1 and 2 are linked to group 2 (with foreign key = group_id in user table) -> right
on the second execute of the script the users with id 1 and 2 are linked to null -> failure
on the third execute of the script the users with id 1 and 2 are linked to group 2 -> right
on the fourth execute of the script the users with id 1 and 2 are linked to null -> failure
and so on
I tried to find the error on my self, but had no luck.
That's what I have found:
On every script-execution the relations to group 2 are first nulled.
If they were allready null für user 1 and 2, the relations are build with an update -> right!; but when this two users had allready this relation they are only nulled and no update is processed.
Thats why there is this strange toggling on the foreign key on every script execution.
Maybe it has something to do with the record_state which is clean, when the relations are allready set in the DB -> no update is executed, but the nulling of all foreign key linked to group 2.
I changed the code on record.php near line 1923 to
if (is_array($value)) { if (isset($value[0]) && ! is_array($value[0])) { $this->unlink($key, array(), true); // parameter set to true foreach ($value as $id) { $this->link($key, $id, true); // parameter set to true } } else { $this->$key->synchronizeWithArray($value); } }With this changed parameters the described bug does not appear, cause the whole state-checking (in UnitOfWork->saveGraph()) is not used and the querys are submitted emidiatly.
Of course this can not be the way, but I hope it helps you a bit to find the error and others who need the synchronizeWithArray method working now.