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

Doctrine doesn't persist empty objects

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major 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

        Vladimir Razuvaev created issue -
        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}
        Jonathan H. Wage made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Can't Fix [ 7 ]
        Jonathan H. Wage made changes -
        Resolution Can't Fix [ 7 ]
        Status Resolved [ 5 ] Reopened [ 4 ]
        Jonathan H. Wage made changes -
        Status Reopened [ 4 ] Resolved [ 5 ]
        Fix Version/s 1.0.0BETA1 [ 10080 ]
        Resolution Fixed [ 1 ]

          People

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

            Dates

            • Created:
              Updated:
              Resolved: