我出了个奇怪的错误。我有一个警报实体,在其中我存储其他实体的集合。我还有一个独立的名为可用性的实体。我不存储在我的警报实体,因为我正在处理它在其他地方。我的确有一个链接到它,我的警报实体
/**
* @var \Nick\AlertBundle\Entity\Alert
*
* @ORM\ManyToOne(targetEntity="Nick\AlertBundle\Entity\Alert")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="alert_id", referencedColumnName="id")
* })
*/
private $alert;
/**
* Set Alert
*
* @param \Nick\AlertBundle\Entity\Alert $alert
* @return Availability
*/
public function setAvailabilityAlert(\Nick\AlertBundle\Entity\Alert $alert = null)
{
$this->alert = $alert;
return $this;
}添加警报时,我的EventListener会对该警报执行一些附加操作。它实现的一个功能是
public function findSeatsOnFlights( $alert, $allInfo, $flights, $seatCodes )
{
$answer = array( );
foreach ( $flights as $flight )
{
$finfo = $allInfo[ $flight ];
$fseats = $finfo["seats"];
foreach ( $seatCodes as $code )
{
if ( array_key_exists( $code, $fseats ) )
{
$answer[] = $code . $fseats[$code];
var_dump("TEST");
//$this->updateAvailabilityTable($alert, $code, "L2J", $finfo["flightNumber"], $fseats[$code]);
}
}
}
return $answer;
}如果函数中包含array_key_exists,则检查所选类的数量是否与数据中的类数组相匹配。因此,如果我搜索C和D类,var_dump会打印两次测试。所以我知道我所有的数据都没问题,而且它确实执行了适当的代码次数。现在,var_dump下面的行注释掉了,代码执行得相当快。如果取消注释,该函数将被调用
function updateAvailabilityTable($alert, $requestedSeat, $pseudo, $flightNumber, $seatCount){
$availability = new Availability();
$availability->setClassLetter($requestedSeat);
$availability->setAlertPseudo($pseudo);
$availability->setFlightNumber($flightNumber);
$availability->setAvailability($seatCount);
$availability->setLastUpdated();
$availability->setAlert($alert);
$em = $this->sc->get('doctrine.orm.entity_manager');
$em->persist($availability);
$em->flush();
}因此,这个函数应该对所选的每个类执行。因此,如果我选择C和D,它应该执行两次,在我的数据库中添加两个可用性行。
问题是,当我测试事物时,当我看到firebug时,post请求就会永远持续下去。当我签出我的数据库表时,已经插入了正确的数据(至少有一个类),但是插入的次数太多了。如果我离开一分钟,我就会有超过100行。
为什么要执行这么多次?
更新它可能与我的侦听器有关
<?php
namespace Nick\AlertBundle\EventListener;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Nick\AlertBundle\Entity\Alert;
use Nick\AlertBundle\Service\UapiService;
class AvailabilityAlertListener
{
protected $api_service;
protected $alertEntity;
protected $em = null;
public function __construct(UapiService $api_service)
{
$this->api_service = $api_service;
}
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof AvailabilityAlert) {
$this->alertEntity = $entity;
}
}
public function postFlush(PostFlushEventArgs $args)
{
$this->em = $args->getEntityManager();
$eventManager = $this->em->getEventManager();
$eventManager -> removeEventListener('postFlush', $this);
if (!empty($this->alertEntity)) {
$this->api_service->addFlightsAction($this->alertEntity);
$this->alertEntity=null;
}
}
}发布于 2015-02-21 20:55:05
问题在于,您的postFlush处理程序正在触发触发另一个刷新事件的代码,该事件再次调用postFlush。您不检查或删除已处理的$this->alertEntity,因此再次触发addFlightsAction。
https://stackoverflow.com/questions/28632221
复制相似问题