Doctrine, namespace and autoload entities

自古美人都是妖i 提交于 2019-12-06 03:48:38

Try to use fully qualified class name with namespace. When you've got error about CG proxy classes try to generate your proxy classes by running

vendor/bin/doctrine orm:generate-proxies

good luck!

If you are using composer, you want to be sure that you have the following 'autoload' in your composer.json:

{
        "require": {
           ...
        },
        "autoload": {
            "psr-0": {"": "Entity/"}
        }
}

This fixed the problem for me. I'm not using any namespace in my entity class though. My composer.json lives in my inc directory and the Entity directory referenced here is at inc/Entity.

Hmm, I've always used the ClassLoader when it comes to using namespaces. I've had so much trouble with it that I 'm using a sort configuration class with all the path/to/src's I need.

E.g. if your path is path/to/src and your classname is Entities\ArticleCat then the directory structure for the location of the file ArticleCat.php is

src \
  Entities \
    ArticleCat.php

Now all you need to do is create a ClassLoader for Entities

$src = 'path/to/src'
$cl = new ClassLoader('Entities', $src);
$cl->register();

and class ArticleCat should now be available using the EntityManager

$articleCat = $em->getRepository('Entities\ArticleCat');
$article = $articleCat->find(21);
echo $article->getName();

or

$article = $em->find('Entities\ArticleCat', 21);
echo $article->getName();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!