2

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.

1
  • Is primary key generated automatically or not? Commented Feb 18, 2015 at 11:04

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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


Update

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

10 Comments

I have added how my id is generated. I initially had this set up with a postPersist, only recently moved to the flush.
Give me a few minutes, the id is coming correctly now with a postPersist. However, I have a different problem occurring now which would lead me to believe the Object is not in the database yet. I will get this added to the original post.
I have redone my original post. The id is fine, but the query does not work on the current id which tells me it is not in the database yet.
@NickPrice what query? Please update your question with some relevant code and errors. It's not first time I told you, isn't it? :)
Oh, I did update the original question, I changed it to the new problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.