加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

当我打开php opcache时,学说很吓人

发布时间:2020-12-13 21:50:52 所属栏目:PHP教程 来源:网络整理
导读:我对Doctrine和 PHP 5.5.6 Opcache有一个非常奇怪的问题.当opcache模块关闭时,一切正常.一旦我打开它,我开始得到以下异常: Fatal error: Uncaught exception 'DoctrineORMMappingMappingException' with message 'Class "AdminModelsUsersRole" is no
我对Doctrine和 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文件中的任何注释.可以想象,这会在依赖于Annotations的任何php库中产生大量无法解释的异常.所以,只需转到你的php.ini,更改下面的行,你就会像新的一样好.我想这也应该进入Doctrine的帮助.

opcache.save_comments=1
opcache.load_comments=1

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读