Doctrine 1
  1. Doctrine 1
  2. DC-39

Linking between one to many relations with synchronizeWithArray fails

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Blocker 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.

      1. DC39TestCase.php
        3 kB
        Marcus Häußler
      2. example.php
        1 kB
        Marcus Häußler

        Activity

        Hide
        Marcus Häußler added a comment -

        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.

        Show
        Marcus Häußler added a comment - 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.
        Hide
        Jonathan H. Wage added a comment -

        Have you tried producing a test case for this? When I tested it I was not able to produce the same behavior in Doctrine standalone. Does this happen in your project or did you try outside of your project with just Doctrine?

        Show
        Jonathan H. Wage added a comment - Have you tried producing a test case for this? When I tested it I was not able to produce the same behavior in Doctrine standalone. Does this happen in your project or did you try outside of your project with just Doctrine?
        Hide
        Marcus Häußler added a comment -

        I tested this with doctrine as a standalone.

        Have you looked at the database entries with an external query-browser or have you just looked at the objects?
        The doctrine_records/collections are build right while the database entries can act with the described behavior.

        I will try to build a testcase but tell me wether you looked in the database to recognize the behavior.

        Show
        Marcus Häußler added a comment - I tested this with doctrine as a standalone. Have you looked at the database entries with an external query-browser or have you just looked at the objects? The doctrine_records/collections are build right while the database entries can act with the described behavior. I will try to build a testcase but tell me wether you looked in the database to recognize the behavior.
        Hide
        Marcus Häußler added a comment -

        Testcase uploaded.

        Show
        Marcus Häußler added a comment - Testcase uploaded.

          People

          • Assignee:
            Jonathan H. Wage
            Reporter:
            Marcus Häußler
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: