Uploaded image for project: 'Doctrine MongoDB ODM'
  1. Doctrine MongoDB ODM
  2. MODM-45

Doctrine doesn't persist empty objects

    Details

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

      Description

      Doctrine won't persist object if it is empty (all fields are nulls). Not sure if it is bug or expected behavior, but sometimes it causes inconvenience. Test case:

      /** @Document(db="tests", collection="tests") */
      class a
      {
          /** @Id */
          protected $id;
      
          /** @String */
          protected $b;
      }
      
      $a = new a();
      $dm->persist($a);
      $dm->flush(); // $a won't be inserted
      

      There is also related issue: when embedding empty object (all fields are nulls) embedded object is not saved.

      /** @Document(db="tests", collection="tests") */
      class a
      {
          /** @Id */
          protected $id;
      
          /** @String */
          protected $tmp = 'WorkaroundToBeSaved';
      
          /** @EmbedOne(targetDocument="b", cascade="all") */
          protected $b;
      
          function getId()  {return $this->id;}
          function getB()   {return $this->b;}
          function setB($b) {$this->b = $b;}
      }
      
      /** @EmbeddedDocument */
      class b
      {
          /** @String */
          protected $val;
          function setVal($val) {$this->val = $val;}
          function getVal() {return $this->val;}
      }
      
      $a = new a();
      $a->setB(new b());
      
      $dm->persist($a);
      $dm->flush();
      $dm->getUnitOfWork()->clear();
      
      $a = $dm->loadByID('a', $a->getId());
      $c = (null !== $a->getB()); 
      var_dump($c); // returns false, while expecting true
      

        Activity

        vladar Vladimir Razuvaev created issue -
        vladar Vladimir Razuvaev made changes -
        Field Original Value New Value
        Description Doctrine won't persist object if it is empty (all fields are nulls). Not sure if it is bug or expected behavior, but sometimes it causes inconvenience. Test case:

        {code}
        /** @Document(db="tests", collection="tests") */
        class a
        {
            /** @Id */
            protected $id;

            /** @String */
            protected $b;
        }

        $a = new a();
        $dm->persist($a);
        $dm->flush(); // $a won't be inserted
        {code}

        Doctrine won't persist object if it is empty (all fields are nulls). Not sure if it is bug or expected behavior, but sometimes it causes inconvenience. Test case:

        {code}
        /** @Document(db="tests", collection="tests") */
        class a
        {
            /** @Id */
            protected $id;

            /** @String */
            protected $b;
        }

        $a = new a();
        $dm->persist($a);
        $dm->flush(); // $a won't be inserted
        {code}


        There is also related issue: when embedding empty object (all fields are nulls) embedded object is not saved.

        {code}
        /** @Document(db="tests", collection="tests") */
        class a
        {
            /** @Id */
            protected $id;

            /** @String */
            protected $tmp = 'WorkaroundToBeSaved';

            /** @EmbedOne(targetDocument="b", cascade="all") */
            protected $b;

            function getId() {return $this->id;}
            function getB() {return $this->b;}
            function setB($b) {$this->b = $b;}
        }

        /** @EmbeddedDocument */
        class b
        {
            /** @String */
            protected $val;
            function setVal($val) {$this->val = $val;}
            function getVal() {return $this->val;}
        }

        $a = new a();
        $a->setB(new b());

        $dm->persist($a);
        $dm->flush();
        $dm->getUnitOfWork()->clear();

        $a = $dm->loadByID('a', $a->getId());
        $c = (null !== $a->getB());
        var_dump($c); // returns false, while expecting true
        {code}
        Hide
        jwage Jonathan H. Wage added a comment -

        Mongo won't let you insert empty documents, so we can't allow it.

        Show
        jwage Jonathan H. Wage added a comment - Mongo won't let you insert empty documents, so we can't allow it.
        Hide
        jwage Jonathan H. Wage added a comment -
        $mongo = new Mongo();
        $doc = array();
        $mongo->dbname->coll->insert($doc);
        

        Causes this error:

        PHP Fatal error:  Uncaught exception 'MongoException' with message 'no elements in doc' in /Users/jwage/Sites/mongodb-odm/test.php:5
        Stack trace:
        #0 /Users/jwage/Sites/mongodb-odm/test.php(5): MongoCollection->insert(Array)
        #1 {main}
          thrown in /Users/jwage/Sites/mongodb-odm/test.php on line 5
        
        MongoException: no elements in doc in /Users/jwage/Sites/mongodb-odm/test.php on line 5
        
        Call Stack:
            0.0004     320216   1. {main}() /Users/jwage/Sites/mongodb-odm/test.php:0
            0.0037     322036   2. MongoCollection->insert() /Users/jwage/Sites/mongodb-odm/test.php:5
        
        Show
        jwage Jonathan H. Wage added a comment - $mongo = new Mongo(); $doc = array(); $mongo->dbname->coll->insert($doc); Causes this error: PHP Fatal error: Uncaught exception 'MongoException' with message 'no elements in doc' in /Users/jwage/Sites/mongodb-odm/test.php:5 Stack trace: #0 /Users/jwage/Sites/mongodb-odm/test.php(5): MongoCollection->insert(Array) #1 {main} thrown in /Users/jwage/Sites/mongodb-odm/test.php on line 5 MongoException: no elements in doc in /Users/jwage/Sites/mongodb-odm/test.php on line 5 Call Stack: 0.0004 320216 1. {main}() /Users/jwage/Sites/mongodb-odm/test.php:0 0.0037 322036 2. MongoCollection->insert() /Users/jwage/Sites/mongodb-odm/test.php:5
        jwage Jonathan H. Wage made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Can't Fix [ 7 ]
        Hide
        vladar Vladimir Razuvaev added a comment -

        I see now. But second case is still possible with mongo and not supported by ODM:

        $a = new a();
        $a->setB(new b());
        $dm->persist($a);
        $dm->flush($a);
        
        /* expecting:
        {
          "_id": "4c69ff3c0f9d50c80e000000",
          "tmp": "WorkaroundToBeSaved",
          "b" => []
        }
        
        * getting:
        {
          "_id": "4c69ff3c0f9d50c80e000000",
          "tmp": "WorkaroundToBeSaved"
        }
        */
        
        Also, if I remove string "WorkaroundToBeSaved" from $a, it won't be saved with flush, although it is not empty (has $b field set).
        
        Show
        vladar Vladimir Razuvaev added a comment - I see now. But second case is still possible with mongo and not supported by ODM: $a = new a(); $a->setB( new b()); $dm->persist($a); $dm->flush($a); /* expecting: { "_id" : "4c69ff3c0f9d50c80e000000" , "tmp" : "WorkaroundToBeSaved" , "b" => [] } * getting: { "_id" : "4c69ff3c0f9d50c80e000000" , "tmp" : "WorkaroundToBeSaved" } */ Also, if I remove string "WorkaroundToBeSaved" from $a, it won't be saved with flush, although it is not empty (has $b field set).
        Hide
        jwage Jonathan H. Wage added a comment -

        This is expected as well since we never store null values or empty arrays.

        Show
        jwage Jonathan H. Wage added a comment - This is expected as well since we never store null values or empty arrays.
        Hide
        jwage Jonathan H. Wage added a comment -

        After a second look we can fix this.

        Show
        jwage Jonathan H. Wage added a comment - After a second look we can fix this.
        jwage Jonathan H. Wage made changes -
        Resolution Can't Fix [ 7 ]
        Status Resolved [ 5 ] Reopened [ 4 ]
        Show
        jwage Jonathan H. Wage added a comment - Fixed by http://github.com/doctrine/mongodb-odm/commit/b3a16496c0576e8f926faec9bbcfd5716006273b
        jwage Jonathan H. Wage made changes -
        Status Reopened [ 4 ] Resolved [ 5 ]
        Fix Version/s 1.0.0BETA1 [ 10080 ]
        Resolution Fixed [ 1 ]

        This list may be incomplete, as errors occurred whilst retrieving source from linked applications:

        • Request to http://www.doctrine-project.org/fisheye/ failed: Error in remote call to 'FishEye 0 (http://www.doctrine-project.org/fisheye/)' (http://www.doctrine-project.org/fisheye) [AbstractRestCommand{path='/rest-service-fe/search-v1/crossRepositoryQuery', params={expand=changesets[0:20].revisions[0:29],reviews, query=MODM-45}, methodType=GET}] : Received status code 503 (Service Temporarily Unavailable)

          People

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

            Dates

            • Created:
              Updated:
              Resolved: