堆栈在Doctrine PostPersist事件监听器

时间:2013-07-11 07:13:50

标签: symfony doctrine event-listener

我的VisitorController班级

public function chooseVisitor($zone)
{
    $em = $this->getDoctrine()->getManager();

    $lvisitor = $em->getRepository('EMRSabaBundle:Personel')->findOneBy(array('status' => '1', 'lastv' => '1', 'zone' => $zone));
    $id = $lvisitor->getId();
    $lvisitor->setLastv(0);


    $newvisitorid = $em->getRepository('EMRSabaBundle:Personel')->findNewVisitor($id);
    $newvisitorid = $newvisitorid[0];
    $newvisitor = $em->getRepository('EMRSabaBundle:Personel')->find($newvisitorid);
    $newvisitor->setLastv(1);

    $em->flush();

    return $newvisitor;
}

public function defineZone($Customer)
{
    $phone = substr($Customer,1);
    switch ($phone)
    {
        case 2:
            $zone = 1;
            break;
        case 4:
            $zone = 1;
            break;
        case 8:
            $zone = 2;
            break;
        case 7:
            $zone = 3;
            break;
    }

    return $zone;
}

我的EventListener

namespace EMR\SabaBundle\EventListener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use EMR\SabaBundle\Entity\Orders;
use EMR\SabaBundle\Entity\Visit;
use EMR\SabaBundle\Controller\VisitController;

class EntityListener {

    public function postPersist(LifecycleEventArgs $args)
    {
        $orders = $args->getEntity();
        $em = $args->getEntityManager();
        if ($orders instanceof Orders){
            if ($orders->getStatus() == 1){
                $VC = new VisitController();
                $zone = $VC->defineZone($orders->getCustomer()->getId());
                $personel = $VC->chooseVisitor($zone);
                $visit = new Visit();
                $visit->setDate(new \DateTime());
                $visit->setStatus(0);
                $visit->setOrders($orders);
                $visit->setPersonel($personel);

                $em->persist($visit);
                $em->flush();
            }
        }
    }
}

&安培;我的services.yml

    entity.listener:
    class: EMR\SabaBundle\EventListener\EntityListener
    tags:
        - { name: doctrine.event_listener, event: postPersist }

我想创造&当订单实体状态为1时,持续新访问 我不知道问题在哪里&如何调试事件监听器

任何人都可以帮我完成这项工作

1 个答案:

答案 0 :(得分:0)

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#postupdate-postremove-postpersist

  

postUpdate,postRemove,postPersist

     

在EntityManager#flush()中调用三个post事件。变化   在这里与数据库中的持久性无关,但是你   可以使用这些事件来更改非可持久化项目,例如非映射项目   字段,日志记录甚至直接映射的关联类   教义。

尝试prePersist事件。