-->

Silex and Doctrine - ORMException: Unknown Entity

2019-06-11 04:26发布

问题:

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?