Search code examples
phpsymfony

Symfony2 - PostFlush Event


I had things set up that through a form you could add an Alert to my database. When this Alert was added, a postFlush event took the Alert and performed a task on it. This all worked perfectly.

I have now changed things. Instead of adding an Alert through a form, I display all possible Alerts they can have. They can then select whatever alerts they want an add these to the database. So whereas before only one Alert was added at a time, now multiple Alerts can be added.

So the problem now is that in my Controller, an Alert is added using a foreach

foreach ($data as $flightNum=>$seat) {
    $alert = new Alert();
    $alert->setLastUpdated();
    $alert->setIsDeleted(0);
    $alert->setAlertStatus('Active');

    $flightNumber = new FlightNumbers();
    $flightNumber->setAvailabilityAlert($alert);
    $flightNumber->setFlightNumber($flightNum);
    $alert->addFlightNumber($flightNumber);
    $em->persist($flightNumber);

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

The problem with this is that my postFlush only seems to execute now for the first Alert that is added. So if I choose three Alerts, the first one has the additional postFlush action performed on it but the other two do not.

I have played about with my postFlush to try using an Array instead, does not seem to work though

class AlertListener
{
    protected $api_service;
    protected  $alertEntity = array();
    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 Alert) {
            array_push($this->alertEntity, $entity);
            var_dump("TEST");
        }
    }

    public function postFlush(PostFlushEventArgs $args)
    {
        $this->em = $args->getEntityManager();
        $eventManager = $this->em->getEventManager();
        $eventManager->removeEventListener('postFlush', $this);

        \Doctrine\Common\Util\Debug::dump($this->alertEntity);

        if (!empty($this->alertEntity)) {
            foreach($this->alertEntity as $alert) {
                $this->api_service->addFlightsAction($alert);
            }
        }

    }
}

Interesting thing is, the var_dump outputs twice if I choose 2 Alerts, which would leave me to believe the correct number of Alerts are added to the Array.

However, the Symfony2 dump of the Object only outputs one Alert, so apparently not. Interesting thing is that if I dump the array in the postPersist function I get loads of Alerts listed. If I dump in the postFlush function, only one Alert is outputted.

How can I handle multiple Alerts here?

Thanks


Solution

  • First of all you shouldn't flush inside a loop, for performance sake flush outside, then get an array of your Alerts in your Postflush event from PostFlushEventArgs , then use them for whatever you want.