$agent = new OrganizationAgent(); $closure = (new AgentViewClosure()) ->setAncestor($agent) ->setDescendant($agent) ->setPath($agent->getId()) ->setLevel(self::DEFAULT_LEVEL); $agent->addDescendantClosure($closure) ->addAncestorClosure($closure); $em->persist($closure); 

$ agent-> getId () - there will be null. How can I pass id?

    2 answers 2

    You can add a method

     public function setId($id) { $this->id = $id; } 

    Thereafter:

     $entity = new Product(); $entity->setId(1); // ... // вот это важно $em->getClassMetaData(get_class($entity))->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator()); $em->persist($entity); $em->flush(); 

    Taken from http://www.ens.ro/2012/07/03/symfony2-doctrine-force-entity-id-on-persist/

    • I added a subscriber to the postPersist there is an id there but the entity closure has to be flashed - the only negative is Serge Esmanovich

    What you need is a "cascade" save, because you created two entities and the first depends on the second.

    Code example:

     <?php class AgentViewClosure { //... /** * Bidirectional - One-To-Many * * @OneToMany(targetEntity="OrganizationAgent", cascade={"persist", "remove"}) */ private $author; //... } 

    Pay special attention to cascade={"persist", "remove"} . Read more here.