Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Upgrading to DoctrineBundle 2.0

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $10.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

Let's look at the latest list of deprecated code. Hmm... there's a lot of stuff related to Doctrine. Ok: two tricky things are happening in the Doctrine world right now that make upgrading to Symfony 5... a bit more confusing. First, DoctrineBundle has a new major version - 2.0. And second, Doctrine itself is being split into smaller packages. Both things are great... but it means there are some extra "moving pieces".

DoctrineBundle & Symfony 5 Compatibility

If you search this page for DoctrineBundle, there's one deprecation: some missing getMetadataDriverClass() method in a DoctrineExtension class. So far, this is nothing new: a third-party library is using some deprecated code... which means that we need to upgrade it.

Google for DoctrineBundle and find its GitHub page. If you did some digging, you'd learn that if you want Symfony 5 support, you need version 2 or higher of this bundle. There's also a version 1.12 that's being maintained... but it won't work with Symfony 5.

Debugging the DoctrineBundle Version

So let's start with our normal, lazy way of upgrading. In your terminal, run:

composer update doctrine/doctrine-bundle

It does upgrade... but only to 1.12.6. So probably we need to go into composer.json and update its version constraint. Search for doctrine/doctrine-bundle. Huh... it's actually not inside our composer.json file. It must be a dependency of something else. Let's find out more. Run:

composer why doctrine/doctrine-bundle

Ok: doctrine-bundle is required by both fixtures-bundle and migrations-bundle. But the original reason that it was installed was because of symfony/orm-pack, which allows version 1 or 2. Remember: orm-pack is a sort of "fake" library that requires other libraries. It gives us an easy way to install Doctrine and some other Doctrine-related libraries.

I want to have more control over doctrine/doctrine-bundle so that I can force version 2 to be used, instead of just 1 or 2. To do that, we can "unpack" the pack. Run:

composer unpack symfony/orm-pack

Very simply, this removes symfony/orm-pack from our composer.json file:

103 lines composer.json
{
... lines 2 - 3
"require": {
... lines 5 - 30
"symfony/orm-pack": "^1.0",
... lines 32 - 42
},
... lines 44 - 101
}

And replaces it with the libraries that it requires:

105 lines composer.json
{
... lines 2 - 3
"require": {
... lines 5 - 8
"doctrine/doctrine-bundle": "^1.6.10|^2.0",
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0",
"doctrine/orm": "^2.5.11",
... lines 12 - 44
},
... lines 46 - 103
}

Change the doctrine-bundle version to just ^2.0:

105 lines composer.json
{
... lines 2 - 3
"require": {
... lines 5 - 8
"doctrine/doctrine-bundle": "^2.0",
... lines 10 - 44
},
... lines 46 - 103
}

Handling Composer Update Problems

Now that we're forcing version 2, let's... see if it will update! Run:

composer update doctrine/doctrine-bundle

And... this does not work. I should've seen this coming. doctrine-bundle can't be updated to version 2 because our project currently has doctrine-fixtures-bundle 3.2, which requires doctrine-bundle 1.6. So apparently we also need to update doctrine-fixtures-bundle. Ok! Copy the library name and say:

composer update doctrine/doctrine-bundle doctrine/doctrine-fixtures-bundle

We may need to change that bundle's version constraints to allow it to upgrade to a new major version:

105 lines composer.json
{
... lines 2 - 45
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0",
... lines 48 - 54
},
... lines 56 - 103
}

I honestly don't really know. My hope is that a newer 3.something version will allow doctrine-bundle 2.0. But when we check on Composer... bah! It didn't work! But this time because of a different library: doctrine-migrations-bundle. That also needs to be updated. No problem: copy its name and add it to the end of our composer update line:

composer update doctrine/doctrine-bundle \
                doctrine/doctrine-fixtures-bundle \
                doctrine/doctrine-migrations-bundle

We're now allowing doctrine-bundle, doctrine-fixtures-bundle and doctrine-migrations-bundle all to update and... it still doesn't work. Sheesh. Let's see: this time it's because doctrine-migrations-bundle requires doctrine/migrations 2.2 and apparently we're locked at a lower version of that. Yea we have 2.1.1.

It's the same problem... again. Well, it's slightly different. We could add doctrine/migrations to the end of our composer update command - or even use doctrine/* - and try it again. That should work.

Or we can add --with-dependencies. This says: allow any of these three bundles to update and allow their dependencies to update. doctrine/migrations is not in our composer.json file: it's a dependency of doctrine-migrations-bundle. Oh, and if you really want the "easy" way out, we could have just ran composer update with no arguments to allow anything to update. But I prefer to update as little as possible at one time. Try the command:

Tip

We need this for our course CI, just ignore this note and follow the tutorial without executing these commands :)

sed -i 's/doctrine-bundle": "^1.11"/doctrine-bundle": "^2.0"/g' ./composer.json
sed -i 's/RegistryInterface $registry/ManagerRegistry $registry/g' ./src/Repository/ApiTokenRepository.php
sed -i 's/use Symfony\\Bridge\\Doctrine\\RegistryInterface/use Doctrine\\Persistence\\ManagerRegistry/g' ./src/Repository/ApiTokenRepository.php
sed -i 's/RegistryInterface $registry/ManagerRegistry $registry/g' ./src/Repository/ArticleReferenceRepository.php
sed -i 's/use Symfony\\Bridge\\Doctrine\\RegistryInterface/use Doctrine\\Persistence\\ManagerRegistry/g' ./src/Repository/ArticleReferenceRepository.php
sed -i 's/RegistryInterface $registry/ManagerRegistry $registry/g' ./src/Repository/ArticleRepository.php
sed -i 's/use Symfony\\Bridge\\Doctrine\\RegistryInterface/use Doctrine\\Persistence\\ManagerRegistry/g' ./src/Repository/ArticleRepository.php
sed -i 's/RegistryInterface $registry/ManagerRegistry $registry/g' ./src/Repository/CommentRepository.php
sed -i 's/use Symfony\\Bridge\\Doctrine\\RegistryInterface/use Doctrine\\Persistence\\ManagerRegistry/g' ./src/Repository/CommentRepository.php
sed -i 's/RegistryInterface $registry/ManagerRegistry $registry/g' ./src/Repository/TagRepository.php
sed -i 's/use Symfony\\Bridge\\Doctrine\\RegistryInterface/use Doctrine\\Persistence\\ManagerRegistry/g' ./src/Repository/TagRepository.php
sed -i 's/RegistryInterface $registry/ManagerRegistry $registry/g' ./src/Repository/UserRepository.php
sed -i 's/use Symfony\\Bridge\\Doctrine\\RegistryInterface/use Doctrine\\Persistence\\ManagerRegistry/g' ./src/Repository/UserRepository.php
composer update doctrine/doctrine-bundle \
                doctrine/doctrine-fixtures-bundle \
                doctrine/doctrine-migrations-bundle \
                --with-dependencies

And... it worked! Yeeeea! Then... exploded at the bottom. We'll talk about that in a minute. First, look back up: it upgraded doctrine-migrations-bundle and doctrine-fixtures-bundle both to new minor versions. So there shouldn't be any breaking changes in those.

The doctrine-bundle upgrade was over a major version - from 1 to 2 - so it shouldn't be a huge surprise that it made our code go bonkers: the new version does have some breaking changes.

Exit: DoctrineCacheBundle

One other thing I want to point out is that doctrine-cache-bundle was removed. That's no longer needed by doctrine and you shouldn't use it anymore either: use Symfony's cache.

Next, let's fix our app to work with DoctrineBundle 2.0 and update its recipe, which contains a few important config changes.

Leave a comment!

0
Login or Register to join the conversation
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": "^7.3.0",
        "ext-iconv": "*",
        "antishov/doctrine-extensions-bundle": "^1.4", // v1.4.2
        "aws/aws-sdk-php": "^3.87", // 3.110.11
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "doctrine/doctrine-bundle": "^2.0", // 2.0.6
        "doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // 2.1.2
        "doctrine/orm": "^2.5.11", // v2.7.2
        "doctrine/persistence": "^1.3.7", // 1.3.8
        "easycorp/easy-log-handler": "^1.0", // v1.0.9
        "http-interop/http-factory-guzzle": "^1.0", // 1.0.0
        "knplabs/knp-markdown-bundle": "^1.7", // 1.8.1
        "knplabs/knp-paginator-bundle": "^5.0", // v5.0.0
        "knplabs/knp-snappy-bundle": "^1.6", // v1.7.0
        "knplabs/knp-time-bundle": "^1.8", // v1.11.0
        "league/flysystem-aws-s3-v3": "^1.0", // 1.0.23
        "league/flysystem-cached-adapter": "^1.0", // 1.0.9
        "league/html-to-markdown": "^4.8", // 4.8.2
        "liip/imagine-bundle": "^2.1", // 2.3.0
        "nexylan/slack-bundle": "^2.1", // v2.2.1
        "oneup/flysystem-bundle": "^3.0", // 3.3.0
        "php-http/guzzle6-adapter": "^2.0", // v2.0.1
        "sensio/framework-extra-bundle": "^5.1", // v5.5.3
        "symfony/asset": "5.0.*", // v5.0.2
        "symfony/console": "5.0.*", // v5.0.2
        "symfony/dotenv": "5.0.*", // v5.0.2
        "symfony/flex": "^1.0", // v1.17.6
        "symfony/form": "5.0.*", // v5.0.2
        "symfony/framework-bundle": "5.0.*", // v5.0.2
        "symfony/mailer": "5.0.*", // v5.0.2
        "symfony/messenger": "5.0.*", // v5.0.2
        "symfony/monolog-bundle": "^3.5", // v3.5.0
        "symfony/security-bundle": "5.0.*", // v5.0.2
        "symfony/sendgrid-mailer": "5.0.*", // v5.0.2
        "symfony/serializer-pack": "^1.0", // v1.0.2
        "symfony/twig-bundle": "5.0.*", // v5.0.2
        "symfony/twig-pack": "^1.0", // v1.0.0
        "symfony/validator": "5.0.*", // v5.0.2
        "symfony/webpack-encore-bundle": "^1.4", // v1.7.2
        "symfony/yaml": "5.0.*", // v5.0.2
        "twig/cssinliner-extra": "^2.12", // v2.12.0
        "twig/extensions": "^1.5", // v1.5.4
        "twig/inky-extra": "^2.12" // v2.12.0
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.0", // 3.3.0
        "fzaninotto/faker": "^1.7", // v1.8.0
        "symfony/browser-kit": "5.0.*", // v5.0.2
        "symfony/debug-bundle": "5.0.*", // v5.0.2
        "symfony/maker-bundle": "^1.0", // v1.14.3
        "symfony/phpunit-bridge": "5.0.*", // v5.0.2
        "symfony/profiler-pack": "^1.0", // v1.0.4
        "symfony/var-dumper": "5.0.*" // v5.0.2
    }
}
userVoice