How to remove a document from a reference document array in the ODM doctrine using mongodb

I have a mapping of php objects in a mongodb document (called Node) to a structure

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

class Node{
    /**
    * @MongoDB\Id
    */
    protected $id;

    /**
    * @MongoDB\String
    */
    protected $domain;

    /**
    * @MongoDB\ReferenceMany(targetDocument="NodeItem",cascade=     
    * {"persist"},simple="true")
    */
    protected $items = array();

    //getter and setters below
}

And the referencing document called NodeItem,

class NodeItem {

  /**
  * @MongoDB\Id
  */
  protected $id;

  /**
  * @MongoDB\String
  */
  protected $name;

  /**
   * @MongoDB\ReferenceOne(targetDocument="Node", cascade={"persist"},    
   *  simple="true")
   */
   protected Node;

   //setter and getters 
}

As reflected in the annotation above 'Node', MANY 'NodeItems' links are stored in the $ items array and 'NodeItems' refer to ONE 'Node'. Thus, these are bidirectional reference collections.

My question is how to efficiently remove multiple "NodeItem" documents from its collection (based on an array of available identifiers), so that deleted NodeItem documents are also removed from the links in the $ items array in 'Node' (cascading deletion, I think this is what am I asking for?).

I wrote a function with this code:

   $qb = $this->dm->createQueryBuilder('SomeBundleBundle:NodeItem');
    /*
     * deletes from NodeItem collection
     */
    foreach($NodeItemsArray as $itemId){
        $qb->remove()->field('id')->equals($itemId)->getQuery()->execute();
    }

NodeItem, $items 'Node' . , {cascade: persist} , , . Symfony 2.

!

+3
2

- onRemove.

, @jmikola, $dm- > remove(), QueryBuilder ( ).


:

//Get the NodeItem you want in the items array and delete it:
$items = $node->getItems();
$dm->remove($items->get(2)); //Remove the third item as an example

:

class CascadeNodeDeleterListener {
        public function preRemove(LifecycleEventArgs $eventArgs) {

        $odm = $eventArgs->getDocumentManager(); /* @var $odm DocumentManager */
        $object = $eventArgs->getDocument();
        if($object instanceOf NodeItem) {
                $node = $object->getNode();
                $items = $node->getItems();
                $items->removeElement($object);
                $class = $dm->getClassMetadata(get_class($node));
                $dm->getUnitOfWork()->recomputeSingleDocumentChangeSet($class, $node);
            }

        }
}

services.yml:

 <service id="listener" class="CascadeNodeDeleterListener">
      <tag name="doctrine.common.event_listener" event="onRemove" />
 </service>

. Doctrine ODM Events.

+1

ODM UnitOfWork. MongoDB (, ). :

db.node_items.remove({"_id": ObjectId("...")})

UnitOfWork ( ), .

, , $nodeItem. DocumentManager::remove() UnitOfWork , , cascade=REMOVE cascade=ALL, . , flush() MongoDB.

, , - DocumentManager::persist(). , Node, NodeItems , Node ( ).

NodeItems Node, cascade=REMOVE $nodeItem->getNode()->getItems()->removeElement($nodeItem) $nodeItem, flush().

, , . ODM Doctrine\ODM\MongoDB\PersistentCollection, . , , Doctrine\Common\Collections\ArrayCollection . , , Collection .

0
source

All Articles