Roel Veldhuizen
Roel Veldhuizen

Reputation: 4723

'X was not found in the chain configured namespaces' in service

I have a service configured in Symfony:

   v_bla_orm.listener.address:
        class: Bla\OrmBundle\EventListener\AddressManager
        calls:
            - [ setContainer, [ @service_container ] ]
        tags:
            - { name: doctrine.event_listener, event: prePersist } 
            - { name: doctrine.event_listener, event: preUpdate }  

When I run my code in a unit test the following exception will occur:

The class 'Bla\OrmBundle\EventListener\AddressManager' was not found in the chain configured namespaces Bla\OrmBundle\Entity, Ivory\GoogleMapBundle\Entity

When stepping through the code I determined that the exception occurs in the following line in AddressManager:

$meta = $em->getClassMetadata(get_class($entity));

The whole function:

public function preUpdate(LifecycleEventArgs $args) {
    $entity = $this->getLatLong($args);
    $em = $args->getEntityManager();
    $uow = $em->getUnitOfWork();
    $meta = $em->getClassMetadata(get_class($entity));
    $uow->recomputeSingleEntityChangeSet($meta, $entity);                  
}

What I don't understand is why should AddressManager be in the chain of namespaces? Since it is not an entity and when stepping step by step through the code, the code gets into the AddressManager?

Also how can I solve this?

Edit

Added lotlong function aswell

 public function getLatLong(LifecycleEventArgs $args) {

        $entity = $args->getEntity();

        if ($entity instanceof \Bla\OrmBundle\Entity\Address) {

            $geocoder = $this->container->get('ivory_google_map.geocoder');
            $string = $entity->getStreet() . ' ' . $entity->getStreetNumber() . ', ' . $entity->getCity() . ' ' . $entity->getCountry();
            $response = $geocoder->geocode($string);
            $results = $response->getResults();

            $entity->setLatitude(null);
            $entity->setLongitude(null);

            foreach ($results as $result) {
                $location = $result->getGeometry()->getLocation();

                $entity->setLatitude($location->getLatitude());
                $entity->setLongitude($location->getLongitude());
                return $entity;
            }
        }
    }

Upvotes: 0

Views: 944

Answers (1)

qooplmao
qooplmao

Reputation: 17759

Your getLatLong is not returning an entity if it's not an instance of \Bla\OrmBundle\Entity\Address meaning that it is essentially return null.

This means that when you are later calling..

$meta = $em->getClassMetadata(get_class($entity));

.. it is really calling..

$meta = $em->getClassMetadata(get_class(null));

.. which means it will return that name of the class that called it, in this case Bla\OrmBundle\EventListener\AddressManager.

There are many ways to sort it but the easiest would be..

public function preUpdate(LifecycleEventArgs $args) {
    if (null === $entity = $this->getLatLong($args)) {
        return;
    }
    $em = $args->getEntityManager();
    ....                  
}

Upvotes: 2

Related Questions