Как обновить коллекцию объектов manyToMany в прослушивателе событий onFlush?

У меня есть этот объект:

apples = new \Doctrine\Common\Collections\ArrayCollection();
        $this->pigs = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function setApples($apples) {

        $this->getApples()->clear();

        foreach ($apples as $apple) {

            $this->addApple($apple);
        }
    }

    public function setPigs($pigs) {

        $this->getPigs()->clear();

        foreach ($pigs as $pig) {

            $this->addPig($pig);
        }
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Set content
     *
     * @param text $content
     */
    public function setContent($content) {
        $this->content = $content;
    }

    /**
     * Get content
     *
     * @return text 
     */
    public function getContent() {
        return $this->content;
    }

    /**
     * Add apples
     *
     * @param Comakai\MyBundle\Entity\Apple $apples
     */
    public function addApple(\Comakai\MyBundle\Entity\Apple $apples) {
        $this->apples[] = $apples;
    }

    /**
     * Get apples
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getApples() {
        return $this->apples;
    }


    /**
    * Add pigs
    *
    * @param Comakai\MyBundle\Entity\Pig $pigs
    */
    public function addPig(\Comakai\MyBundle\Entity\Pig $pigs) {
        $this->pigs[] = $pigs;
    }

    /**
    * Get pigs
    *
    * @return Doctrine\Common\Collections\Collection 
    */
    public function getPigs() {
        return $this->pigs;
    }
}

и этот слушатель:

getEntityManager();
        $uow = $em->getUnitOfWork();

        foreach ($uow->getScheduledEntityInsertions() AS $entity) {

            $this->save($entity, $em, $uow);

        }

        foreach ($uow->getScheduledEntityUpdates() AS $entity) {

            $this->save($entity, $em, $uow);

        }
    }

    public function save($entity, $em, $uow) {

        if ($entity instanceof Stuff) {

            $pigRepository = $em->getRepository('Comakai\MyBundle\Entity\Pig');
            $content = $entity->getContent();

            preg_match_all('/@@ pig:(\d+) @@/i', $content, $matches);
            $entity->getPigs()->clear();

            foreach($matches[1] as $pigID) {

                $pig = $pigRepository->find($pigID);

                if(!empty($pig)) {

                    $entity->addPig($pig);

                }

            }

            $entity->setContent($content);

            $meta = $em->getClassMetadata(get_class($entity));
            $uow->recomputeSingleEntityChangeSet($meta, $entity);
            $uow->computeChangeSet($meta, $entity);

        }
    }

}

И он отлично работает, если коллекция Apple пуста, но если в ней есть какой-то элемент, я получаю ошибку дублирования.

Как мне сообщить UnitOfWork, что я хочу только пересчитать сбор свиньи?

ОБНОВЛЕНИЕ

Существует новое событие preFlush ( https://github.com/doctrine/doctrine2/pull/169), и я думаю, что такие вещи можно делать там. Этого PR нет в той ветке, которой я пользуюсь, но давайте попробуем!

5
задан coma 6 June 2012 в 15:21
поделиться