Php Symfony2-正在将inifite数据添加到数据库中

Php Symfony2-正在将inifite数据添加到数据库中,php,symfony,Php,Symfony,我犯了一个奇怪的错误。我有一个警报实体,并在其中存储其他实体的集合。我还有一个单独的实体,叫做availability。我不会将其存储在我的警报实体中,因为我正在其他地方处理它。我确实有一个链接到我的警报实体 /** * @var \Nick\AlertBundle\Entity\Alert * * @ORM\ManyToOne(targetEntity="Nick\AlertBundle\Entity\Alert") * @ORM\JoinColumns({ * @ORM\Joi

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

/**
 * @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_的地方,该函数检查我选择的类与数据中的类数组匹配的类的数量。所以如果我搜索C和D类,这个var_转储会打印测试两次。所以我知道我所有的数据都是正常的,并且它执行代码的次数是正确的。现在注释掉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;
        }
    }
}

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

事件侦听器侦听的是哪个实体类?它已添加到警报类中。您可以粘贴警报实体类吗?没有问题,已添加。我没有指向此实体中可用性类的链接我不会像创建其他实体那样为其创建阵列集合。第一段代码显示了我是如何将可用性实体链接回警报实体的。我想看看可用性课程:抱歉,有点误解了。postFlush函数不只是调用addFlightsAction并传递一个变量吗?当我将一个var_转储放在postFlush中时,它只执行一次。@NickPrice这可能是对的:我没有注意到您没有检查实体类。在你的听众中,你总是要检查我们的课程,你可能会陷入你不想陷入的境地……但我不是在我的后期作者中这样做的吗?如果我没记错的话,我无法访问postFlush中的实体?我猜它在进入var_转储之前会递归到另一个flush/postFlush周期,直到它放弃。。。如果将var_转储放在addFlightsAction调用之前,或者更好地使用调试器,会发生什么?