是否有与 PHP 5.6 兼容的 Doctrine 2 版本

Is there a Doctrine 2 version that is compatible with PHP 5.6

我有一个使用 Doctrine >=2.2.3,<2.4-dev 的 Symfony 2.3 应用程序。我将生产服务器升级到 PHP 5.6,这导致 FOSUserBundle 出现以下错误:

Warning: Erroneous data format for unserializing 'air\cms2Bundle\Entity\User' in /vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php on line 833

Fatal error: __clone method called on non-object in /vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php on line 837

实体只是 FOSUserBundle BaseUser 的扩展,如图 here. The FOSUserBundle BaseUser class can be found here

在做了一些初步研究后,我发现错误是由 PHP 5.6 和 Doctrine 2 兼容性问题引起的 Serializable in PHP 5.6. The Doctrine developers have fixed the issue (see GitHub pull request),但我仍然没有确定我必须在 composer.json 中要求哪个版本的 Doctrine 2 才能获得修复,我也不确定它与哪个版本的 Symfony 2 兼容。

我现在的 composer.json:

"require": {
        "php": ">=5.3.3",
        "symfony/symfony": "2.3.*",
        "doctrine/orm": ">=2.2.3,<2.4-dev",
        "doctrine/doctrine-bundle": "1.2.*",
        ...
},

更新:

我已将 composer.json 更新为:

"require": {
    "php": ">=5.6.0",
    "symfony/symfony": "2.6.*",
    "doctrine/orm": "~2.5",
    "doctrine/doctrine-bundle": "1.2.*",
    ...
},

但不幸的是,这会导致无法解决的依赖性问题:

Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Conclusion: don't install doctrine/orm v2.5.4
    - Conclusion: remove doctrine/doctrine-bundle v1.2.0
    - Conclusion: remove doctrine/dbal v2.4.5
    - Conclusion: don't install doctrine/orm v2.5.3
    - Conclusion: don't install doctrine/orm v2.5.2
    - Conclusion: don't install doctrine/orm v2.5.1
    - Conclusion: don't install doctrine/dbal v2.4.5
    - Conclusion: don't install doctrine/dbal v2.5.2|install doctrine/dbal v2.4.5
    - Conclusion: don't install doctrine/dbal v2.5.3|install doctrine/dbal v2.4.5
    - Conclusion: don't install doctrine/dbal v2.5.4|install doctrine/dbal v2.4.5
    - Installation request for doctrine/doctrine-bundle 1.2.* -> satisfiable by doctrine/doctrine-bundle[v1.2.0].
    - Installation request for doctrine/orm ~2.5 -> satisfiable by doctrine/orm[v2.5.0, v2.5.1, v2.5.2, v2.5.3, v2.5.4].
    - doctrine/doctrine-bundle v1.2.0 requires doctrine/dbal >=2.2,<2.5-dev -> satisfiable by doctrine/dbal[v2.4.5, 2.2.0, 2.2.1, 2.2.2, 2.3.0, 2.3.1, 2.3.2, 2.3.3, 2.3.4, 2.3.5, v2.4.0, v2.4.1, v2.4.2, v2.4.3, v2.4.4].
    - Can only install one of: doctrine/dbal[v2.5.0, 2.2.0].
    - Can only install one of: doctrine/dbal[v2.5.0, 2.2.1].
    - Can only install one of: doctrine/dbal[v2.5.0, 2.2.2].
    - Can only install one of: doctrine/dbal[v2.5.0, 2.3.0].
    - Can only install one of: doctrine/dbal[v2.5.0, 2.3.1].
    - Can only install one of: doctrine/dbal[v2.5.0, 2.3.2].
    - Can only install one of: doctrine/dbal[v2.5.0, 2.3.3].
    - Can only install one of: doctrine/dbal[v2.5.0, 2.3.4].
    - Can only install one of: doctrine/dbal[v2.5.0, 2.3.5].
    - Can only install one of: doctrine/dbal[v2.5.0, v2.4.0].
    - Can only install one of: doctrine/dbal[v2.5.0, v2.4.1].
    - Can only install one of: doctrine/dbal[v2.5.0, v2.4.2].
    - Can only install one of: doctrine/dbal[v2.5.0, v2.4.3].
    - Can only install one of: doctrine/dbal[v2.5.0, v2.4.4].
    - doctrine/orm v2.5.0 requires doctrine/dbal >=2.5-dev,<2.6-dev -> satisfiable by doctrine/dbal[v2.5.0, v2.5.1, v2.5.2, v2.5.3, v2.5.4].
    - Conclusion: don't install doctrine/dbal v2.5.1

我也尝试将我的 composer.json 更新为

"require": {
    "php": ">=5.6.0",
    "symfony/symfony": "2.6.*",
    "doctrine/common": "~2.5",
    ...
},

但这会在 composer 完成更新后导致 app/console clear:cache 运行 出错。

Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Removing doctrine/orm (v2.4.8)
  - Removing doctrine/doctrine-bundle (v1.2.0)
  - Removing doctrine/dbal (v2.4.5)
  - Removing jdorn/sql-formatter (v1.2.17)
Writing lock file
Generating autoload files
> Incenteev\ParameterHandler\ScriptHandler::buildParameters
Updating the "app/config/parameters.yml" file
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache
PHP Fatal error:  Class 'Doctrine\Bundle\DoctrineBundle\DoctrineBundle' not found in /vagrant/logbookWebApp-data/app/AppKernel.php on line 16
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-update-cmd event terminated with an exception


  [RuntimeException]                                                         
  An error occurred when executing the "'cache:clear --no-warmup'" command.  


update [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--lock] [--no-plugins] [--no-custom-installers] [--no-autoloader] [--no-scripts] [--no-progress] [--with-dependencies] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [-i|--interactive] [--] [<packages>]...

实际上,您可以完美地使用 "~2.5" 等版本的 Doctrine 和 PHP 5.6
对于低于 5.4 的 PHP 版本,Doctrine 2.4.8 肯定是最后一个兼容版本。

您提供的见解过多,信息不足。
请准确说明您的错误。

更新

要修复此错误,
执行以下命令

php -r "echo PHP_VERSION_ID;"

或者创建一个包含以下内容的简单 php 脚本:

<?php echo PHP_VERSION_ID; ?>

然后,执行它。对于我的 PHP (5.6.16) 版本,它 returns :

50616

现在,获取结果并转到第 827 行附近的 vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php,并将 newInstance 方法替换为:

public function newInstance()
{
  if ($this->_prototype === null) {
       if (PHP_VERSION_ID === YOURPHPVERSIONHERE || PHP_VERSION_ID === 50429 || PHP_VERSION_ID === 50513) {
          $this->_prototype = $this->reflClass->newInstanceWithoutConstructor();
      } else {
          $this->_prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
      }
  }
   return clone $this->_prototype;
}

这解决了您机器的 PHP 版本的问题,使用 Symfony 2.3 中默认安装的版本的学说。

这不是一个理想的解决方案,因为当您 re-clone 存储库或 re-install 供应商时,修复将被压缩。