Ich habe diese Einheit:Wie kann man eine manyToMany-Auflistung einer Entität im onFlush-Ereignis-Listener aktualisieren?
<?php
namespace Comakai\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM,
Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
*/
class Stuff {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
*/
private $content;
/**
* @ORM\ManyToMany(targetEntity="Apple", cascade={"persist"})
*/
private $apples;
/**
* @ORM\ManyToMany(targetEntity="Pig")
*/
private $pigs;
public function __construct() {
$this->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;
}
}
und diese Zuhörer:
<?php
namespace Comakai\MyBundle\Listener;
use Comakai\MyBundle\Util\SluggerParser
Doctrine\ORM\Event\OnFlushEventArgs,
Comakai\MyBundle\Entity\Stuff,
Comakai\MyBundle\Entity\Apple,
Comakai\MyBundle\Entity\Pig;
class Listener {
/**
* @param \Doctrine\ORM\Event\OnFlushEventArgs $ea
*/
public function onFlush(OnFlushEventArgs $ea) {
$em = $ea->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);
}
}
}
Und es funktioniert gut, wenn Apfel Sammlung leer ist, aber wenn es einige Artikel hat ich einen doppelten Fehler.
Wie kann ich dem UnitOfWork mitteilen, dass ich nur die Sammlung des Schweins neu berechnen möchte?
UPDATE
Es gibt ein neues Vorspülen Ereignis (https://github.com/doctrine/doctrine2/pull/169) und ich denke, diese Art von Dingen, es getan werden kann. Diese PR ist nicht in der Branche, die ich benutze, aber lass es uns versuchen!
Sorry, aber das Vorspülen mein Problem gelöst. Ändern der UoW ist eine Art von Hacking und sehr chaotisch, kann es Sie verrückt mit Sammlungen. – coma
Koma, stimme ich zu. Ihre Frage bestand darin, Sachen in onFlush() zu tun, und das ist der einzige Weg, dies zu tun. Wenn Ihr Anwendungsfall stattdessen in preFlush() implementiert werden kann, ist das großartig! – jmlnik
Nun ... es sieht so aus, als würde das PreFlush-Ereignis so ziemlich alles Mögliche tun ... alle geplanten Listen sind leer. – coma