在您的创建控制器中:
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->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);
} } }