Search code examples
mongodbsymfonyphp-7doctrine-odm

Doctrine ODM - PHP7 - Can not use Int/String as Class Name


During the process of upgrading from PHP 5.5 to PHP 7.0 a problem has been encountered with the Symfony project.

The steps from http://www.doctrine-project.org/2016/06/09/odm-1-1-0-and-1-0-6.html have been followed to ensure the mongo-ext works with doctrine. There are no compatibility upgrades occurring.

However the following exception occurs:

PHP Fatal error:  Cannot use 'String' as class name as it is reserved in 
... vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Mapping/Annotations/String.php on line 26      

The issue is most likely related to a configuration issue in composer.json

Which requirement is preventing composer update from installing the correct doctrine/mongodb?

PHP Version 7.0.13-0ubuntu0.16.04.1 (cli) ( NTS )

{
        "name": "foo/bar",
        "license": "proprietary",
        "type": "project",
        "autoload": {
            "psr-4": {
                "": "src/"
            },
            "classmap": [
                "app/AppKernel.php",
                "app/AppCache.php"
            ]
        },
        "autoload-dev": {
            "psr-4": {
                "Tests\\": "tests/"
            }
        },
        "require": {
            "php": "^7.0",
            "symfony/symfony": "3.2.*",
            "doctrine/orm": "^2.5",
            "doctrine/doctrine-bundle": "^1.6",
            "doctrine/doctrine-cache-bundle": "^1.3",
            "symfony/swiftmailer-bundle": "^2.3",
            "symfony/monolog-bundle": "^2.8",
            "sensio/distribution-bundle": "^5.0",
            "sensio/framework-extra-bundle": "^3.0.2",
            "incenteev/composer-parameter-handler": "^2.0",
            "friendsofsymfony/user-bundle": "~2.0@dev",
            "symfony/assetic-bundle": "^2.7",
            "liip/imagine-bundle": "^1.4",
            "doctrine/mongodb-odm": "^1.1",
            "doctrine/mongodb-odm-bundle": "^3.1",
            "guzzlehttp/guzzle": "~6.0",
            "querypath/QueryPath": ">=3.0.0",
            "alcaeus/mongo-php-adapter": "^1.0",
            "ext-mongo": "*"
        },
        "require-dev": {

            "sensio/generator-bundle": "^3.0",
            "symfony/phpunit-bridge": "^2.7"
        },
        "scripts": {
            "post-install-cmd": [
                "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
                "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
                "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
                "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
                "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
                "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
            ],
            "post-update-cmd": [
                "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
                "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
                "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
                "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
                "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
                "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
            ]
        },
        "extra": {
            "symfony-app-dir": "app",
            "symfony-bin-dir": "bin",
            "symfony-var-dir": "var",
            "symfony-web-dir": "web",
            "symfony-tests-dir": "tests",
            "symfony-assets-install": "relative",
            "incenteev-parameters": {
                "file": "app/config/parameters.yml"
            }
        }
    }

Solution

  • Turns out a document in a bundle still had a annotation reference.

    Search the code base for any of the following:

    \String
    \Int
    \Bool
    \Float
    

    and replace with:

    \Field(type="string")
    \Field(type="int")
    \Field(type="bool")
    \Field(type="float")
    

    Beware the Alias

    use Doctrine\ODM\MongoDB\Mapping\Annotations AS MongoDB;
    

    In my code I prefixed the search with MongoDB, i.e. MongoDB\Int became MongoDB\Field(type="int")