Reputation: 963
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.
Upvotes: 2
Views: 252
Reputation: 29912
postPersist is enaugh, I know you have tried but I'm sure you are messing up something. This because:
postPersist - The postPersist event occurs for an entity after the entity has been made persistent. It will be invoked after the database insert operations. Generated primary key values are available in the postPersist event.
from http://doctrine-orm.readthedocs.org/en/latest/reference/events.html
So, to me, you haven't assigned a value for PK to your entity. Or something is not correctly configured
After OP updated the question, is pretty clear that only way to reach what he wants is to listen on postFlush()
event.
I also suggest to pass only integer $id
to addFlightsAction()
as it seems that you do nothing with whole $entity
object
Upvotes: 2
Reputation: 2801
OnFlush is triggered/catched before actually flushing the entities. So no id is available yet.
Try to use postFlush event instead. You will find all available events in doctrine documentation.
http://doctrine-orm.readthedocs.org/en/latest/reference/events.html
Upvotes: 2