'在链配置的命名空间中找不到X'在服务中

时间:2014-07-11 13:30:37

标签: php symfony doctrine-orm

我在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 }  

当我在单元测试中运行代码时,将发生以下异常:

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

当单步执行代码时,我确定异常发生在AddressManager的以下行中:

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

整个功能:

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);                  
}

我不明白为什么AddressManager应该在命名空间链中?由于它不是一个实体,并且在逐步执行代码时,代码进入AddressManager

另外我该如何解决这个问题?

修改

添加了lotlong功能

 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;
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

如果getLatLong不是\Bla\OrmBundle\Entity\Address的实例,则null不返回实体,这意味着它实际上是返回$meta = $em->getClassMetadata(get_class($entity));

这意味着当你稍后打电话时..

Bla\OrmBundle\EventListener\AddressManager

..它真的在呼唤..

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

..这意味着它将返回调用它的类的名称,在本例中为public function preUpdate(LifecycleEventArgs $args) { if (null === $entity = $this->getLatLong($args)) { return; } $em = $args->getEntityManager(); .... }

有很多方法可以对它进行排序,但最简单的方法是......

{{1}}