Doctrine MongoDB ODM
  1. Doctrine MongoDB ODM
  2. MODM-29

Persisting changes ordering of elements in embedded collection in some cases

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.0.0ALPHA2
    • Fix Version/s: 1.0.0BETA2
    • Component/s: Persister
    • Labels:
      None

      Description

      After changing ordering of elements in collection, it is persisted incorrectly (with different ordering than expected).

      Here is a test case:

      /** @Document(collection="tests", db="tests") */
      class doc
      {
          /** @Id */
          protected $id;
      
          /** @EmbedMany(targetDocument="emb") */
          protected $collection;
      
          function __construct($c) {$this->set($c);}
      
          function set($c) {$this->collection = $c;}
          function get() {return $this->collection;}
      }
      
      /** @EmbeddedDocument */
      class emb
      {
          /** @String */
          protected $val;
      
          function __construct($val) {$this->val = $val;}
          function get() {return $this->val;}
      }
      
      $collection = new ArrayCollection(array(
          new emb('0'),
          new emb('1'),
          new emb('2')
      ));
      
      // TEST CASE:
      $doc = new doc($collection);
      
      $dm->persist($doc);
      $dm->flush();
      
      // place element '0' after '1'
      $collection = new ArrayCollection(array(
          $collection[1],
          $collection[0],
          $collection[2]
      ));
      
      $doc->set($collection);
      
      $dm->persist($doc);
      $dm->flush();
      
      $dm->refresh($doc);
      
      foreach($doc->get() as $value) {
          echo $value->get() . "\n";
      }
      
      // expecting: 
      // 1
      // 0
      // 2
      
      // getting: 
      // 2
      // 1
      // 0
      

      Is it an issue? Or is it supposed that ordering should be resolved in application code?

        Activity

        Hide
        Jonathan H. Wage added a comment -

        So the "'collection.1.val' => 'tmp'," part succeeds and the rest is ignored? Obviously the 'collection.1.val' => 'tmp', part should not be there.

        Show
        Jonathan H. Wage added a comment - So the "'collection.1.val' => 'tmp'," part succeeds and the rest is ignored? Obviously the 'collection.1.val' => 'tmp', part should not be there.
        Hide
        Jonathan H. Wage added a comment -

        Hi, to make things easier would you mind making the test cases in the Doctrine test suite? Every test case you provide is great and it works but I have to spend a few minutes modifying the code and putting it into a test case in phpunit.

        Show
        Jonathan H. Wage added a comment - Hi, to make things easier would you mind making the test cases in the Doctrine test suite? Every test case you provide is great and it works but I have to spend a few minutes modifying the code and putting it into a test case in phpunit.
        Hide
        Jonathan H. Wage added a comment -

        Why are you expecting 1, 0, 2? Wouldn't the change to 'tmp' need to be persisted as well?

        Show
        Jonathan H. Wage added a comment - Why are you expecting 1, 0, 2? Wouldn't the change to 'tmp' need to be persisted as well?
        Hide
        Jonathan H. Wage added a comment -

        Fixed here http://github.com/doctrine/mongodb-odm/commit/485fbdfb7f85ecb290530ea0fc9c55a6dca91e19

        I think the expected values and order would be "1, tmp, 2"

        Show
        Jonathan H. Wage added a comment - Fixed here http://github.com/doctrine/mongodb-odm/commit/485fbdfb7f85ecb290530ea0fc9c55a6dca91e19 I think the expected values and order would be "1, tmp, 2"
        Hide
        Vladimir Razuvaev added a comment -

        Yeah, sorry, expecting "1, tmp, 2" of course.

        As for tests - I'll setup Doctrine tests on my side. How should I deliver test cases then? Via github? Or just copy-paste phpunit tests here?

        Show
        Vladimir Razuvaev added a comment - Yeah, sorry, expecting "1, tmp, 2" of course. As for tests - I'll setup Doctrine tests on my side. How should I deliver test cases then? Via github? Or just copy-paste phpunit tests here?

          People

          • Assignee:
            Jonathan H. Wage
            Reporter:
            Vladimir Razuvaev
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: