我在教义和PHP 5.5.6 Opcache
方面遇到了一个非常奇怪的问题。关闭opcache
模块时一切正常。打开它后,我开始出现以下异常:
Fatal error: Uncaught exception 'DoctrineORMMappingMappingException' with message 'Class "AdminModelsUsersRole" is not a valid entity or mapped super class.' in vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php on line 336
( ! ) DoctrineORMMappingMappingException: Class "AdminModelsUsersRole" is not a valid entity or mapped super class. in vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php on line 336
Call Stack
# Time Memory Function Location
1 0.0000 128016 {main}( ) ../app.php:0
2 0.0185 615020 CoreBootstrap->handle( ) ../app.php:53
3 0.0210 695744 call_user_func_array ( ) ../Bootstrap.php:111
4 0.0210 695976 AdminControllersDashboardController->indexAction( ) ../Bootstrap.php:111
5 0.0210 696028 DoctrineORMEntityManager->getRepository( ) ../DashboardController.php:25
6 0.0210 696072 DoctrineORMRepositoryDefaultRepositoryFactory->getRepository( ) ../EntityManager.php:759
7 0.0210 696176 DoctrineORMRepositoryDefaultRepositoryFactory->createRepository( ) ../DefaultRepositoryFactory.php:50
8 0.0210 696200 DoctrineORMEntityManager->getClassMetadata( ) ../DefaultRepositoryFactory.php:67
9 0.0210 696420 DoctrineCommonPersistenceMappingAbstractClassMetadataFactory->getMetadataFor( ) ../EntityManager.php:295
10 0.0213 699628 DoctrineCommonPersistenceMappingAbstractClassMetadataFactory->loadMetadata( ) ../AbstractClassMetadataFactory.php:211
11 0.0224 781128 DoctrineORMMappingClassMetadataFactory->doLoadMetadata( ) ../AbstractClassMetadataFactory.php:318
12 0.0224 782824 DoctrineORMMappingDriverAnnotationDriver->loadMetadataForClass( ) ../ClassMetadataFactory.php:117
我相信我的实体定义正确 - 正如我在开头提到的,一旦我禁用 opcache,一切都按预期工作。为了初始化 Doctrine,我使用了这段代码:
// $this->getEntityPaths() returns an array of existing absolute paths, each of which is checked with realpath().
// $this->getProxyPath() returns a string with the absolute path, checked with realpath()
// $this->getInDevelopment() returns boolean and is set to TRUE
$config = Setup::createAnnotationMetadataConfiguration($this->getEntityPaths(), $this->getInDevelopment(), $this->getProxyPath());
$config->setAutoGenerateProxyClasses(true);
$dbParams = array(
'driver' => 'pdo_pgsql',
'host' => $this->getHost(),
'port' => $this->getPort(),
'user' => $this->getUser(),
'password' => $this->getPass(),
'dbname' => $this->getName()
);
$this->db = EntityManager::create($dbParams, $config);
我也尝试过这种配置:
$config = new Configuration;
$config->setProxyDir($this->getProxyPath());
$config->setProxyNamespace('DoctrineProxies');
$config->setAutoGenerateProxyClasses(true);
$driverImpl = $config->newDefaultAnnotationDriver($this->getEntityPaths());
$config->setMetadataDriverImpl($driverImpl);
$config->setMetadataCacheImpl(new DoctrineCommonCacheArrayCache());
$config->setQueryCacheImpl(new DoctrineCommonCacheArrayCache());
// the connection configuration
$dbParams = array(
'driver' => 'pdo_pgsql',
'host' => $this->host,
'port' => $this->port,
'user' => $this->user,
'password' => $this->pass,
'dbname' => $this->name
);
$this->db = EntityManager::create($dbParams, $config);
这是实体
/**
* Roles
*
* @Table(name="roles")
* @Entity
*/
class Role
{
/**
* @Id
* @Column(name="role_id", type="integer", nullable=false)
* @GeneratedValue(strategy="IDENTITY")
*/
private $role_id;
/**
* @Column(name="name", type="string", length=100, nullable=false)
*/
private $name;
}
为了简单起见,我AutoGenerateProxyClasses
了,但为了确保我也通过生成代理对其进行了测试。不幸的是,我得到了同样的例外。我不确定如何解决这个问题以及问题到底是什么。是教义错误吗?为什么打开OpCache
会导致Doctrine
异常并且关闭它工作正常?我是否遗漏了配置部分或实体中的某些内容?
经过大量的调试,我终于设法理解了为什么 Doctrine 在设置 OpCache 的情况下无法正常工作。显然,默认情况下,opcache 不会存储或加载您在 php 文件中的任何注释。可以想象,这会导致任何依赖于注释的 php 库中出现大量无法解释的异常。所以只要去你的php.ini
,改变下面的行,你就会像新的一样好。我想这也应该进入教义帮助。
opcache.save_comments=1
opcache.load_comments=1