我的应用程序代理中有4个实体代理,Facilitator,Entry和EntryImage。一个机构有多个协调人,他们进入系统,为每个条目发布多个EntryImages。
我有一个postPersist监听器,它根据实体执行操作,当我保存代理或辅助器但不保存Entry或EntryImage时,该监听器运行良好。这是下面的代码
<?php
namespace ACME\MyBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use ACME\MyBundle\Entity\Agency;
use ACME\MyBundle\Entity\Facilitator;
use ACME\MyBundle\Entity\Entry;
use Application\Sonata\UserBundle\Entity\User;
use Twig_Environment as Environment;
class UserListener
{
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$userManager = $this->container->get('fos_user.user_manager');
$em = $args->getEntityManager();
if ($entity instanceof Agency || $entity instanceof Facilitator)
{
$templateFile = "ACMEMyBundle:Default:email.html.twig";
$templateContent = $this->container->get('twig')->loadTemplate($templateFile);
$password = $entity->randomPassword();
$user = $userManager->createUser();
$user->setUsername($entity->getEmail());
$user->setEmail($entity->getEmail());
$user->setPlainPassword($password);
$user->setEnabled(true);
$userManager->updateUser($user, true);
if ($entity instanceof Agency)
{
$group = $em->getRepository('ApplicationSonataUserBundle:Group')->findOneBy(array('id' => 2));
$name = $entity->getName();
$email = array($entity->getContactEmail(),$entity->getAlternativeContactEmail());
}
if ($entity instanceof Facilitator)
{
$group = $em->getRepository('ApplicationSonataUserBundle:Group')->findOneBy(array('id' => 3));
$name = $entity->getFirstName().' '.$entity->getMiddleNames().' '.$entity->getSurname();
$email = $entity->getEmail();
}
$body = $templateContent->render(array('name' => $name, 'password' => $password, 'username' => $entity->getEmail()));
$message = \Swift_Message::newInstance()
->setSubject('Registration to OLX Moving Cheese Portal')
->setFrom(array('movingcheese@localhost.com' => 'Moving Cheese'))
->setTo($entity->getEmail())
->setCC($email)
->setContentType('text/html')
->setReplyTo(array('marvin@localhost.com', 'rita@localhost.com'))
->setBody($body)
;
$this->container->get('mailer')->send($message);
$current_user = $this->container->get('security.context')->getToken()->getUser();
$u = $userManager->findUserByUsername($entity->getEmail());
$u->addGroup($group);
$entity->setUser($u);
if ($entity instanceof Facilitator)
{
$agency = $em->getRepository('ACMEMyBundle:Agency')->findOneBy(array('user' => $current_user));
$entity->setAgency($agency);
$entity->setCreatedBy($current_user->getId());
}
$em->persist($u);
$em->persist($entity);
$em->flush();
}
if ($entity instanceof Entry)
{
$u = $this->container->get('security.context')->getToken()->getUser();
$entity->setUser($u);
$em->persist($entity);
$em->flush();
}
}
}
问题是,当我保存协调人或代理商时,以下行评估正常
if ($entity instanceof Agency || $entity instanceof Facilitator)
但是当我尝试保存和Entry对象时,即使转储$ entity显然已经传递了一个对象,下面的这行也没有评估。
if ($entity instanceof Entry)
我不知道这是否有任何意义,但我可能会提到我已将EntryImageAdmin嵌入EntryAdmin表单