I have a situation I am trying to resolve. I have a controller with a standard createAction
public function createAction(Request $request)
{
try {
$em = $this->getDoctrine()->getManager();
$alert = new AvailabilityAlert();
$alert->setLastUpdated();
$alert->setIsDeleted(0);
$alert->setAlertStatus('Active');
$em->persist($alert);
$em->flush();
return new JsonResponse('Success');
}catch (Exception $e) {
}
}
I then have an event listener for this action
<?php
namespace Nick\AlertBundle\EventListener;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Nick\AlertBundle\Entity\AvailabilityAlert;
use Nick\AlertBundle\Service\UapiService;
class AvailabilityAlertListener
{
protected $api_service;
public function __construct(UapiService $api_service)
{
$this->api_service = $api_service;
}
public function onFlush(OnFlushEventArgs $args)
{
$em = $args->getEntityManager();
foreach ($em->getUnitOfWork()->getScheduledEntityInsertions() as $entity) {
if ($entity instanceof AvailabilityAlert) {
var_dump($entity->getId());
$this->api_service->addFlightsAction($entity);
}
}
}
}
This event listener calls addFlightAction. This is simply
public function addFlightsAction($alert){
$em = $this->sc->get('doctrine.orm.entity_manager');
$worldspanCommand = $em->getRepository("NickAlertBundle:AvailabilityAlert")->getSpecificAlert($alert->getId());
}
This calls a custom repo and is using the id of the newly created alert. The function is
public function getSpecificAlert($id)
{
$active = "Active";
return $this->getEntityManager()
->createQuery(
'SELECT a.id, a.searchCommand,
GROUP_CONCAT(DISTINCT c.classLetter) AS classes,
GROUP_CONCAT(DISTINCT p.pseudo) AS pseudos,
GROUP_CONCAT(DISTINCT f.flightNumber) AS flight_number
FROM NickAlertBundle:AvailabilityAlert a
JOIN NickAlertBundle:AvailabilityAlertBookingClass c
WITH a.id = c.availabilityAlert
JOIN NickAlertBundle:AvailabilityAlertPseudos p
WITH a.id = p.availabilityAlert
JOIN NickAlertBundle:AvailabilityAlertFlightNumbers f
WITH a.id = f.availabilityAlert
WHERE a.alertStatus = :active
AND a.id = :id
GROUP BY a.id'
)
->setParameter('active', $active)
->setParameter('id', $id)
->getResult();
}
If I var_dump the result of the above query, I get an empty response. If I change the setParameter for id to $id-1 then it gives me the correct response but for the last added alert, not the current one. So the query works, but not with the current id. So this tells me by the time this query is executed, the alert is not in the database?
UPDATE
I cant find much information on postFlush, but this does not seem to fire the var_dump
public function postFlush(PostFlushEventArgs $args)
{
$em = $args->getEntityManager();
foreach ($em->getUnitOfWork()->getScheduledEntityInsertions() as $entity) {
if ($entity instanceof AvailabilityAlert) {
var_dump("TEST");
$this->api_service->addFlightsAction($entity);
}
}
}
I dont get any error though.