Doctrine ORM in eZ Publish 5.3

eZ Publish 5.3 comes with Doctrine DBAL enabled, but unfortunately, Doctrine ORM is still not available officially. No need to despair, though: it’s rather easy to enable Doctrine ORM not only for the Symfony Stack, but also for use in legacy extensions (e.g. if you need to provide a panel for the admin backoffice). As this feature is (as far as I’ve seen) not documented yet, this blog article should help you on the right trail.

Enabling Doctrine ORM

At first, you’ll have to add the Doctrine ORM package to composer.json (

"require": {
    "doctrine/doctrine-bundle": "1.2.*",
    "doctrine/orm": "2.4.*",

Then, you’ll need to update your dependencies:

$ /usr/bin/php composer.phar update

Doctrine ORM needs some configuration: Add to ezpublish.yml

        auto_mapping: true

See for further information.

And that, basically, is it – Doctrine ORM is now available to your project in it’s full glory. Now you may proceed similar to Symfony2 projects:

Creating an Entity

– Add Bundle

$ php ezpublish/console generate:bundle --namespace=Acme/MyBundle

– Add Bundle to EzPublishKernel.php

– Setup Entity class in MyBundle/Entity/MyEntity.php with Mapping information as described in,

– Check if Doctrine Mapping finds the Entity class

$ php ezpublish/console doctrine:mapping:info

– Generate Getters and Setters

$ php ezpublish/console doctrine:generate:entities Acme/MyBundle/Entity/MyEntity

– Generate DB table

$ php ezpublish/console doctrine:schema:update --force

Access Doctrine ORM from eZ Publish 5


* Inside a controller:

$doctrine = $this->getDoctrine();
$doctrineRepository = $doctrine->getRepository('MyBundle:MyEntity');

– Simple:

$object = $doctrineRepository->find($id);
// or
$dictionary = $doctrineRepository->findAll();

– Complex:

$query = $doctrineRepository->createQueryBuilder('s')
    ->where(' = :id')
    ->setParameter('id', $id)

$dictionary = $query->getResult();

* Inside a Twig extension:

– Inject Doctrine Registry:

        class: AcmeBundleTwigMyExtension
        arguments: [@doctrine]
            - { name: twig.extension }

– Pick it up in constructor:

* Constructs our extension and loads it with access to the eZ Publish repository and config
* @param DoctrineBundleDoctrineBundleRegistry $doctrineRegistry
public function __construct(Registry $doctrineRegistry) {
    $this->doctrine = $doctrineRegistry;

– Use in custom functions/filters:

$doctrineRepository = $this->doctrine->getRepository('MyBundle:MyEntity');
// use as above

Access Doctrine ORM from eZ Publish 5 Legacy

– Inside the legacy stack, Doctrine is available through the Service Container (

$container = ezpKernel::instance()->getServiceContainer();

/** @var $repository eZPublishAPIRepositoryRepository */
$repository = $container->get( 'ezpublish.api.repository' );
$doctrine = $container->get('doctrine');

$query = $doctrine->getRepository('MyBundle:MyEntity')
    ->orderBy('s.term', 'ASC')

// var_dump($query->getSql());

$result = $query->getArrayResult();

[fblike style=“button_count“ showfaces=“false“ width=“90″ verb=“like“ font=“arial“ float=“left“] [fbshare type=“button“ float=“left“] [google_plusone size=“medium“ float=“left“] [twitter style=“horizontal“ float=“left“ lang=“de“] [linkedin_share style=“right“ float=“left“]

Leave a comment


Bitte geben Sie den Text vor: