Symfony2-将数据添加到数据库中


Symfony2 - inifite data being added to database

我发生了一个奇怪的错误。我有一个Alert实体,其中存储了其他实体的集合。我还有一个单独的实体,叫做可用性。我不会将其存储在我的Alert实体中,因为我正在其他地方处理它。我确实有一个链接到它,我的警报实体通过

/**
 * @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会打印两次TEST。所以我知道我所有的数据都是好的,而且它确实会执行代码适当的次数。现在,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,它应该执行两次,向数据库中添加2行Availability。

问题是,当我测试东西时,当我看到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;
        }
    }
}

问题是您的postFlush处理程序正在触发另一个刷新事件的代码,该事件再次调用postFlush。您没有检查或删除已处理的$this->alertEntity,因此再次激发addFlightsAction