Php 注释@条令\ORM\Mapping\Entity“;类内实体\USER\u用户不存在,或无法自动加载
我希望在我的Zend Framework应用程序中结合使用原则2和“l3pp4rd/DoctrineExtensions”。 但我只收到以下错误消息: 类Entities\USER\u USER中的批注“@Doctrine\ORM\Mapping\Entity”不存在,或无法自动加载 application\bootstrap.phpPhp 注释@条令\ORM\Mapping\Entity“;类内实体\USER\u用户不存在,或无法自动加载,php,zend-framework,doctrine-orm,Php,Zend Framework,Doctrine Orm,我希望在我的Zend Framework应用程序中结合使用原则2和“l3pp4rd/DoctrineExtensions”。 但我只收到以下错误消息: 类Entities\USER\u USER中的批注“@Doctrine\ORM\Mapping\Entity”不存在,或无法自动加载 application\bootstrap.php protected function _initDoctrine() { require_once('Doctrine/Common/ClassLo
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位于
application\models\USER\u 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;
}
}
应用程序\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
...
有什么办法可以让它工作吗?通过回答以下问题来解决: 已添加到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
...
我还删除了
$classLoader = array(new \Doctrine\Common\ClassLoader('Doctrine'), 'loadClass');
$autoloader->pushAutoloader($classLoader, 'Doctrine\\');
来自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');
}
谢谢你阅读我的问题 我刚刚遇到了与Zend Framework 2相同的问题,解决方案几乎相同。只需确保在应用程序安装期间执行上述语句即可。这可以在Module.php中轻松完成:
类模块
{
// ...
/**
*正在引导上执行的事件
*/
启动时的公共函数(事件$e)
{
AnnotationRegistry::registerFile('path/to/ORM/Mapping/Driver/doctrineanotations.php');
}
}
以下代码片段将使用现有的自动加载程序配置查找实体类,而无需硬编码任何路径
use Doctrine\Common\Annotations\AnnotationRegistry;
AnnotationRegistry::registerLoader('class_exists');
这是Zend Framework 2的条令模块使用的方法,至少在2.1版的条令中。它适用于使用注释注册表的任何东西,包括PHPCR-ODM之类的文档映射器
以下是条令模块中的用法:
这解决了我在实体上使用其他非教义注释的问题。