class ClassA { /** * @ORM \Column(type="integer", name="ID") * @ORM \Id * @ORM \GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM \Column(type="string", name="Name", nullable=true, length=30) */ protected $name; /** * @ORM \Column(type="integer", name="Limit", nullable=true) */ protected $limit; protected $container public function setContainer(ContainerInterface $container = null) { $this->container = $container; } /** * * @return \Doctrine\ORM\EntityManager */ protected function getEntityManager() { return $this->container->get('doctrine')->getManager('default'); } /** * @ORM \PreUpdate */ protected function onLimitChanged(PreUpdateEventArgs $eventArgs){ $em = $this->getEntityManager(); if ($eventArgs->getEntity() instanceof ClassA) { if ($eventArgs->hasChangedField('limit')) { if(!$tmp = $em->getRepository('ACMECommonBundle:ClassB')->findOneBy(array('name' => 'limit_changed'))) { $tmp = new \ACME\CommonBundle\Entity\ClassB(); $tmp->setName('limit_changed'); } $tmp->setValue($this->id); $em->persist($tmp); $em->flush(); } } *** } class ClassB { /** * @ORM \Column(type="string", name="name", length=30) * @ORM \Id */ protected $name protected $value; *** } |
2 answers
You can not push the container or any other services in essence. Handle entities "outside" by pushing them into services.
|
Having checked the documentation, I found out that it is impossible to make a persist in PreUpdate .
|