Silex and Doctrine - ORMException: Unknown Entity namespace alias [semi fixed]

雨燕双飞 提交于 2020-01-03 06:06:48

问题


I'm trying to use the Doctrine components in my app built using silex. I was able to get it to work - well almost.

I have my "User" entity and the corresponding repository

When doing

$app['em']->getRepository('Foo\Entity\User')->findAll()

works as expected, however when trying to make a custom query

      $this->getEntityManager()
                ->createQuery(
                'SELECT
                    u
                FROM 
                    Foo:User u
                WHERE c.id = :x'
                )
                ->setParameter('x',$in)
                ->getResult();

I get this exception

ORMException: Unknown Entity namespace alias 'Foo'

Please ignore the fact that I can make a select with findById() or findBy(array('id'=>$in)) the problem is with the custom query

This are my configs

$app['orm.em.options'] = array(
    'mappings' => array(
        array(
            'type' => 'annotation',
            'namespace' => 'Foo\Entity',     
            'alias' => 'core',
            'path' => '%app.path%/src/Foo/Entity',
            'use_simple_annotation_reader' => false,
        )
));

and

$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src/Foo/Entity"));
$params = $app['db.options'];
$app['em'] = EntityManager::create($params, $config);

After some research possible solutions:

  • auto_mapping: true => tried, no success
  • registering the namespace => tried, not sure if right was done so may be the solution, please advice how to do it right
  • besides all this I have tried to search for git repos with similar 'usage' but didn't get it :(

UPDATE

for the moment I use the following line in my query and it works

FROM 
InstaLikes\Entity\User u

回答1:


When you create custom queries, you should use the fully namespace, in this case: Foo\Entity\User




回答2:


I am assuming you have checked the alias you have given in the mappings options?

$app['orm.em.options'] = array(
    'mappings' => array(
        array(
            'type' => 'annotation',
            'namespace' => 'Foo\Entity',     
            'alias' => 'core',
            'path' => '%app.path%/src/Foo/Entity',
            'use_simple_annotation_reader' => false,
        )
));

Should that alias option not be set to Foo instead?



来源:https://stackoverflow.com/questions/25842487/silex-and-doctrine-ormexception-unknown-entity-namespace-alias-semi-fixed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!