0

这是我的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 时创建并保持新访问我不知道问题出在哪里以及如何调试事件侦听器

谁能帮我完成这项工作

4

1 回答 1

0

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

postUpdate、postRemove、postPersist

这三个发布事件在 EntityManager#flush() 中调用。此处的更改与数据库中的持久性无关,但是您可以使用这些事件来更改非持久性项目,例如非映射字段、日志记录,甚至是由 Doctrine 直接映射的关联类。

尝试 prePersist 事件。

于 2013-07-11T09:07:01.170 回答