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 (https://doc.ez.no/display/EZP/Content+Repository+configuration):
"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
doctrine:
orm:
auto_mapping: true
See https://symfony.com/doc/2.0/reference/configuration/doctrine.html 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 https://symfony.com/doc/current/book/doctrine.html, https://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html
– 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
(https://symfony.com/doc/current/book/doctrine.html)
- 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('s.id = :id')
->setParameter('id', $id)
->getQuery();
$dictionary = $query->getResult();
- Inside a Twig extension:
– Inject Doctrine Registry:
services:
ita.twig.my_extension:
class: AcmeBundleTwigMyExtension
arguments: [@doctrine]
tags:
- { 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 (https://doc.ez.no/display/EZP/Legacy+code+and+features):
$container = ezpKernel::instance()->getServiceContainer();
/** @var $repository eZPublishAPIRepositoryRepository */
$repository = $container->get( 'ezpublish.api.repository' );
$doctrine = $container->get('doctrine');
$query = $doctrine->getRepository('MyBundle:MyEntity')
->createQueryBuilder('s')
->select('s')
->orderBy('s.term', 'ASC')
->getQuery();
// var_dump($query->getSql());
$result = $query->getArrayResult();