注释“@ Doctrine\ORM\Mapping\Entity”在类Entities\USER_User不存在或不能自动加载 [英] The annotation "@Doctrine\ORM\Mapping\Entity" in class Entities\USER_User does not exist, or could not be auto-loaded

查看:220
本文介绍了注释“@ Doctrine\ORM\Mapping\Entity”在类Entities\USER_User不存在或不能自动加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Zend Framework-Application中使用Doctrine 2和l3pp4rd / DoctrineExtensions。
但是我只收到以下错误消息:


注释@ Doctrine\ORM\Mapping\Entity在类Entities\USER_User不存在或不能自动加载。


application\bootstrap.php

  protected function _initDoctrine(){

require_once('Doctrine / Common / ClassLoader.php');
$ autoloader = Zend_Loader_Autoloader :: getInstance();

$ classLoader = array(new \Doctrine\Common\ClassLoader('Doctrine'),'loadClass');
$ autoloader-> pushAutoloader($ classLoader,'Doctrine\\\'');

$ classLoader = new \Doctrine\Common\ClassLoader('Entities',
realpath(Zend_Registry :: get('config') - > resources-> entityManager- >连接 - >实体),'loadClass');
$ autoloader-> pushAutoloader(array($ classLoader,'loadClass'),'Entities');

$ classLoader = new \Doctrine\Common\ClassLoader('Repositories',
realpath(Zend_Registry :: get('config') - > resources-> entityManager- >连接 - >实体),'loadClass');
$ autoloader-> pushAutoloader(array($ classLoader,'loadClass'),'Repositories');
}

library \My\Resource\Entitymanager.php在 http://www.gediminasm.org/article/annotation-reference

  class My_Resource_Entitymanager extends Zend_Application_Resource_ResourceAbstract 
{

public function init()
{

//警告:设置,假设自动装载机设置为
//从application.ini文件中配置设置
$ zendConfig = new Zend_Config($ this-> getOptions());
//全局使用缓存驱动,在生产中使用APC或memcached
$ cache = new Doctrine\Common\Cache\ArrayCache;
//标准注释阅读器
$ annotationReader = new Doctrine\Common\Annotations\AnnotationReader;
$ cachedAnnotationReader = new Doctrine\Common\Annotations\CachedReader(
$ annotationReader,//使用reader
$ cache //和缓存驱动
);
//创建用于元数据读取的驱动程序链
$ driverChain = new Doctrine\ORM\Mapping\Driver\DriverChain();
//仅加载超类元数据映射到驱动链
//还注册Gedmo注释。注意:您可以个性化它
Gedmo\DoctrineExtensions :: registerAbstractMappingIntoDriverChainORM(
$ driverChain ,//我们的元数据驱动程序链,钩入
$ cachedAnnotationReader //我们的缓存注释阅读器
);

//现在我们要注册我们的应用程序实体
//因为我们需要另一个用于Entity命名空间的元数据驱动
$ annotationDriver = new Doctrine\ORM\ Mapping\Driver\AnnotationDriver(
$ cachedAnnotationReader,//我们的缓存注释阅读器
$ zendConfig-> connection->实体//要查找的路径
);
//注意:应用程序的驱动程序实体可以不同,Yaml,Xml或任何
//注册注册驱动程序实体名称空间
$ driverChain-> addDriver($ annotationDriver,'实体);

//一般ORM配置
$ config = new Doctrine\ORM\Configuration;
$ config-> setProxyDir($ zendConfig-> connection-> proxies-> location);
$ config-> setProxyNamespace($ zendConfig-> connection-> proxies-> ns);
$ config-> setAutoGenerateProxyClasses($ zendConfig-> connection-> proxies-> generate); //这可以基于生产配置。
//注册元数据驱动
$ config-> setMetadataDriverImpl($ driverChain);
//使用我们已经初始化的缓存驱动程序
$ config-> setMetadataCacheImpl($ cache);
$ config-> setQueryCacheImpl($ cache);

//创建事件管理器和挂钩优先扩展侦听器
$ evm = new Doctrine\Common\EventManager();
// gedmo扩展侦听器,删除哪些不使用

// sluggable
$ sluggableListener = new Gedmo\Sluggable\SluggableListener;
//您应该将已使用的注释读取器设置为监听器,以避免为映射驱动程序创建新的注释器
$ sluggableListener-> setAnnotationReader($ cachedAnnotationReader);
$ evm-> addEventSubscriber($ sluggableListener);

//树
$ treeListener = new Gedmo\Tree\TreeListener;
$ treeListener-> setAnnotationReader($ cachedAnnotationReader);
$ evm-> addEventSubscriber($ treeListener);

// loggable,未在例子中使用
$ loggableListener = new Gedmo\Loggable\LoggableListener;
$ loggableListener-> setAnnotationReader($ cachedAnnotationReader);
$ evm-> addEventSubscriber($ loggableListener);

// timestampable
$ timestampableListener = new Gedmo\Timestampable\TimestampableListener;
$ timestampableListener-> setAnnotationReader($ cachedAnnotationReader);
$ evm-> addEventSubscriber($ timestampableListener);

//可翻译
$ translatableListener = new Gedmo\Translatable\TranslatableListener;
//当前翻译语言环境应该从会话或钩子设置到监听器
//最重要的是在实体管理器被刷新之前
$ translatableListener-> setTranslatableLocale('en');
$ translatableListener-> setDefaultLocale('en');
$ translatableListener-> setAnnotationReader($ cachedAnnotationReader);
$ evm-> addEventSubscriber($ translatableListener);

//可排序,不用于例如
$ sortableListener = new Gedmo\Sortable\SortableListener;
$ sortableListener-> setAnnotationReader($ cachedAnnotationReader);
$ evm-> addEventSubscriber($ sortableListener);

// mysql设置名称UTF-8(如果需要)
$ evm-> addEventSubscriber(new Doctrine\DBAL\Event\Listeners\MysqlSessionInit());
// DBAL连接
$ connection = array(
'driver'=>{$ zendConfig-> connection-> driver},
'host'= "$ {$ zendConfig-> connection-> host},
'dbname'=>{$ zendConfig-> connection-> dbname},
'user'= "$ {$ zendConfig-> connection-> user},
'password'=>{$ zendConfig-> connection-> password}
);
//最后,创建实体管理器
$ em = Doctrine\ORM\EntityManager :: create($ connection,$ config,$ evm);
Zend_Registry :: set('em',$ em);
return $ em;

}

}

application\ models\USER_User.php

 命名空间实体; 

使用Doctrine\ORM\Mapping作为ORM;
/ **
* @ ORM\Entity
* @ ORM\HasLifecycleCallbacks
* /
class USER_User
{
/ *
* @ ORM\Id
* @ ORM\Column(type =integer)
* @ ORM\GeneratedValue(strategy =AUTO)
* /
private $ id;
/ **
*检索用户ID
* /
public function getId()
{
return $ this-> id;
}
}

application \configs\application.ini

  ... 
includePaths.library = APPLICATION_PATH/../library/Doctrine
...
pluginPaths.My_Resource =我的/资源
...
autoloaderNamespaces [] =Doctrine
autoloaderNamespaces [] =Gedmo
autoloaderNamespaces [] = Symfony
autoloaderNamespaces [] =我的
...
resources.entityManager.connection.driver =pdo_mysql
resources.entityManager.connection.host =localhost
resources.entityManager.connection.dbname =test
resources.entityManager.connection.user =test
resources.entityManager.connection.password =test
resources.entityManager.connection.entities = APPLICATION_PATH/ models
resources.entityManager.connection.proxies.location = APPLICATION_PATH/ models / Proxies
resources.entityManager.connection.proxies.ns =Proxies

;根据教义手册,这对于
应该是正确的;开发和假生产
resources.entityManager.connection.proxies.generate = true
...

任何想法可以得到这个工作?

解决方案

解决方案: b doctrine2 autloader with cli必须使用AnnotationRegistry



添加到库\My\Resource\Entitymanager.php

 使用Doctrine\\ \\Common\Annotations\AnnotationRegistry; 
class My_Resource_Entitymanager extends Zend_Application_Resource_ResourceAbstract
{

public function init()
{
AnnotationRegistry :: registerFile(__ DIR__。'/../../教义/ ORM /映射/驱动程序/ DoctrineAnnotations.php');
//警告:安装程序假设自动装载机设置为
//从application.ini文件配置设置
...

我也删除了

  $ classLoader = array(new \ Doctrine\Common\ClassLoader('Doctrine'),'loadClass'); 
$ autoloader-> pushAutoloader($ classLoader,'Doctrine\\\'');

from application\bootstrap.php




I'd like to use Doctrine 2 combined with "l3pp4rd/DoctrineExtensions" in my Zend Framework-Application. But I only get the following error message:

The annotation "@Doctrine\ORM\Mapping\Entity" in class Entities\USER_User does not exist, or could not be auto-loaded.

application\bootstrap.php

  protected function _initDoctrine() {

    require_once('Doctrine/Common/ClassLoader.php');
    $autoloader = Zend_Loader_Autoloader::getInstance();

    $classLoader = array(new \Doctrine\Common\ClassLoader('Doctrine'), 'loadClass');
    $autoloader->pushAutoloader($classLoader, 'Doctrine\\');

    $classLoader = new \Doctrine\Common\ClassLoader('Entities',
      realpath(Zend_Registry::get('config')->resources->entityManager->connection->entities), 'loadClass');
    $autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Entities');

    $classLoader = new \Doctrine\Common\ClassLoader('Repositories',
      realpath(Zend_Registry::get('config')->resources->entityManager->connection->entities), 'loadClass');
    $autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Repositories');
  }

library\My\Resource\Entitymanager.php found at http://www.gediminasm.org/article/annotation-reference

class My_Resource_Entitymanager extends Zend_Application_Resource_ResourceAbstract
{

    public function init()
    {

        // WARNING: setup, assumes that autoloaders are set
        // configuration settings from the application.ini file
        $zendConfig = new Zend_Config($this->getOptions());
        // globally used cache driver, in production use APC or memcached
        $cache = new Doctrine\Common\Cache\ArrayCache;
        // standard annotation reader
        $annotationReader = new Doctrine\Common\Annotations\AnnotationReader;
        $cachedAnnotationReader = new Doctrine\Common\Annotations\CachedReader(
            $annotationReader, // use reader
            $cache // and a cache driver
        );
        // create a driver chain for metadata reading
        $driverChain = new Doctrine\ORM\Mapping\Driver\DriverChain();
        // load superclass metadata mapping only, into driver chain
        // also registers Gedmo annotations.NOTE: you can personalize it
        Gedmo\DoctrineExtensions::registerAbstractMappingIntoDriverChainORM(
            $driverChain, // our metadata driver chain, to hook into
            $cachedAnnotationReader // our cached annotation reader
        );

        // now we want to register our application entities,
        // for that we need another metadata driver used for Entity namespace
        $annotationDriver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver(
            $cachedAnnotationReader, // our cached annotation reader
            $zendConfig->connection->entities // paths to look in
        );
        // NOTE: driver for application Entity can be different, Yaml, Xml or whatever
        // register annotation driver for our application Entity namespace
        $driverChain->addDriver($annotationDriver, 'Entities');

        // general ORM configuration
        $config = new Doctrine\ORM\Configuration;
        $config->setProxyDir($zendConfig->connection->proxies->location);
        $config->setProxyNamespace($zendConfig->connection->proxies->ns);
        $config->setAutoGenerateProxyClasses($zendConfig->connection->proxies->generate); // this can be based on production config.
        // register metadata driver
        $config->setMetadataDriverImpl($driverChain);
        // use our allready initialized cache driver
        $config->setMetadataCacheImpl($cache);
        $config->setQueryCacheImpl($cache);

        // create event manager and hook prefered extension listeners
        $evm = new Doctrine\Common\EventManager();
        // gedmo extension listeners, remove which are not used

        // sluggable
        $sluggableListener = new Gedmo\Sluggable\SluggableListener;
        // you should set the used annotation reader to listener, to avoid creating new one for mapping drivers
        $sluggableListener->setAnnotationReader($cachedAnnotationReader);
        $evm->addEventSubscriber($sluggableListener);

        // tree
        $treeListener = new Gedmo\Tree\TreeListener;
        $treeListener->setAnnotationReader($cachedAnnotationReader);
        $evm->addEventSubscriber($treeListener);

        // loggable, not used in example
        $loggableListener = new Gedmo\Loggable\LoggableListener;
        $loggableListener->setAnnotationReader($cachedAnnotationReader);
        $evm->addEventSubscriber($loggableListener);

        // timestampable
        $timestampableListener = new Gedmo\Timestampable\TimestampableListener;
        $timestampableListener->setAnnotationReader($cachedAnnotationReader);
        $evm->addEventSubscriber($timestampableListener);

        // translatable
        $translatableListener = new Gedmo\Translatable\TranslatableListener;
        // current translation locale should be set from session or hook later into the listener
        // most important, before entity manager is flushed
        $translatableListener->setTranslatableLocale('en');
        $translatableListener->setDefaultLocale('en');
        $translatableListener->setAnnotationReader($cachedAnnotationReader);
        $evm->addEventSubscriber($translatableListener);

        // sortable, not used in example
        $sortableListener = new Gedmo\Sortable\SortableListener;
        $sortableListener->setAnnotationReader($cachedAnnotationReader);
        $evm->addEventSubscriber($sortableListener);

        // mysql set names UTF-8 if required
        $evm->addEventSubscriber(new Doctrine\DBAL\Event\Listeners\MysqlSessionInit());
        // DBAL connection
        $connection = array(
            'driver'   => "{$zendConfig->connection->driver}",
            'host'     => "{$zendConfig->connection->host}",
            'dbname'   => "{$zendConfig->connection->dbname}",
            'user'     => "{$zendConfig->connection->user}",
            'password' => "{$zendConfig->connection->password}"
        );
        // Finally, create entity manager
        $em = Doctrine\ORM\EntityManager::create($connection, $config, $evm);
        Zend_Registry::set('em', $em);
        return $em;

    }

}

application\models\USER_User.php

namespace Entities;

use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class USER_User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * Retrieve user id
     */
    public function getId()
    {
        return $this->id;
    }
}

application\configs\application.ini

...
includePaths.library = APPLICATION_PATH "/../library/Doctrine"
...
pluginPaths.My_Resource = "My/Resource"
...
autoloaderNamespaces[] = "Doctrine"
autoloaderNamespaces[] = "Gedmo"
autoloaderNamespaces[] = "Symfony"
autoloaderNamespaces[] = "My"
...
resources.entityManager.connection.driver = "pdo_mysql"
resources.entityManager.connection.host = "localhost"
resources.entityManager.connection.dbname = "test"
resources.entityManager.connection.user = "test"
resources.entityManager.connection.password = "test"
resources.entityManager.connection.entities = APPLICATION_PATH "/models"
resources.entityManager.connection.proxies.location = APPLICATION_PATH "/models/Proxies"
resources.entityManager.connection.proxies.ns = "Proxies"

; According to Doctrine manual, this should be true for
; development, and false for production
resources.entityManager.connection.proxies.generate = true
...

Any ideas to get this working?

解决方案

Solved with the answer of this question: doctrine2 autloader with cli must use AnnotationRegistry

added to library\My\Resource\Entitymanager.php

use Doctrine\Common\Annotations\AnnotationRegistry; 
class My_Resource_Entitymanager extends Zend_Application_Resource_ResourceAbstract
{

    public function init()
    {
        AnnotationRegistry::registerFile(__DIR__ . '/../../Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php');
        // WARNING: setup, assumes that autoloaders are set
        // configuration settings from the application.ini file
        ...

I also removed

$classLoader = array(new \Doctrine\Common\ClassLoader('Doctrine'), 'loadClass');
$autoloader->pushAutoloader($classLoader, 'Doctrine\\');

from application\bootstrap.php

Thanks for reading my question!

这篇关于注释“@ Doctrine\ORM\Mapping\Entity”在类Entities\USER_User不存在或不能自动加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆