使用Symfon2.0和Doctrine,我尝试执行以下操作:
<something>/row_id
我怎样才能轻松做到这一点?
我一直在尝试Doctrine Livecycle回调。但是:
有任何线索吗?
还有其他方法吗?
答案 0 :(得分:1)
在您的创建控制器中:
if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($entity); $em->flush(); $entity->setCode($code); $em->persist($entity); $em->flush(); return $this->redirect($this->generateUrl('entity_show' ,array('id' => $entity->getId()))); }
您需要添加第二个块($ entity-&gt; setCode ...),并且需要根据您的目的进行自定义
或者,您可以使用听众:
<?php namespace ormed\ormedBundle\Listener; use Doctrine\ORM\Event\OnFlushEventArgs; use Symfony\Component\DependencyInjection\Container; class LastModifiedListener { private $container; public function __construct(Container $container){$this->container = $container;} public function onFlush(OnFlushEventArgs $eventArgs) { $entityManager = $eventArgs->getEntityManager(); $unitOfWork = $entityManager->getUnitOfWork(); foreach ($unitOfWork->getScheduledEntityInsertions() AS $entity) { $entity->setCode( $code ); $entityManager->persist($entity); $classMetadata = $entityManager->getClassMetadata(get_class($entity)); $unitOfWork->recomputeSingleEntityChangeSet($classMetadata, $entity); } } }