Yii2 & Doctrine ORM simple integration

https://unsplash.com/photos/IKHvOlZFCOg

This all comes from a consultancy we’re currently doing. Our client need, as a stepping-stone towards more modern architecture and a more modern software stack, to integrate Doctrine ORM into their current Yii2 installation. We found some extensions: Yii2-Doctrine and Doctrine2Orm but after examining them further, they don’t seem like a good fit. Mainly for the lack of maintenance of both of them. In fact, they don’t currently have active development anymore.

Therefore, we started digging into Yii2 documentation trying to find the most simple way to integrate both of them. And this is what we achieved.

Yii2 Dependency Injection for the win

Although compared to other frameworks like Symfony or Laravel it’s a bit simpler, yet Yii2 Dependency Injection has enough features to integrate Doctrine ORM in a straightforward way. All the Doctrine ORM entities should be placed in a /src/DomainModel folder and all the XML mapping files should be placed in a src/Infrastructure/Persistence/doctrine-mappings

We’ve added a new file di.php into standard Yii2 config folder

Then it’s really easy to reuse this configuration into theweb.php or console.php files.

Example web.php file

And then from a controller

The full source code can be found at https://github.com/rigorguild/yii2-doctrine.

This is one of the cases we help with. If you need help on how to take on your technical challenges, we can surely help. Don’t hesitate to contact us!

Rigor Guild

It’s all talk until the code runs

Rigor Guild

Today, still wanted by the government they survive as soldiers of fortune. If you have a problem, if no one else can help, and if you can find them… maybe you can hire the Rigor Guild Team!

Christian Soronellas

Written by

XP Practitioner — Author of https://leanpub.com/ddd-in-php

Rigor Guild

Today, still wanted by the government they survive as soldiers of fortune. If you have a problem, if no one else can help, and if you can find them… maybe you can hire the Rigor Guild Team!

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store