Use the Gedmo Doctrine Extensions in Zend Framework 2

Since October 2011 I maintained a small module for the Gedmo Doctrine extensions. These extensions were quite useful and with the Doctrine modules from Marco Pivetta (Ocramius) it was fairly easy to build a module configuring and autoloading the Gedmo extensions.

However, the Zend Framework changed drastically a couple of days back: a service manager, Composer integration and many more features which are not relevant for this blog post. This change made my module unneccessary and because maintaining it is unneccessary as well, I will drop the module.

When you use Composer, it is easy to integrate the extensions in your application. First you need to create a dependency on the extensions. If you haven’t a composer.json file in your project, create one with the following contents:

{
  "require": {
    "gedmo/doctrine-extensions": "dev-master"
  }
}

Then you need to fetch composer and resolve the dependencies. This is done by the command (on Linux and OS X, you need to Google for the Windows commands):

wget http://getcomposer.org/composer.phar && php composer.phar install

If you have already a composer.json file and just want to update, add the gedmo/doctrine-extensions requirement and run php composer.phar update.

The second part is to register the Gedmo annotations to the Doctrine annotations registry. This should just happen anywhere, for example in the init() function of your Application module. You need to import the registry:

use Doctrine\Common\Annotations\AnnotationRegistry;

Then you add this in your init() method:

$namespace = 'Gedmo\Mapping\Annotation';
$lib = 'vendor/gedmo/doctrine-extensions/lib';
AnnotationRegistry::registerAutoloadNamespace($namespace, $lib);

This is almost everything you need to do. Make sure you include the autoload.php file inside the vendor directory, because that file makes sure the Gedmo files are loaded properly. That’s all, you don’t need my module to integrate the Gedmo DoctrineExtensions in your Zend Framework 2 project anymore!

Update (configuration):

As Mike pointed out in the comments below, you still need some configuration per module to get your entities connected to the Gedmo listeners. This is something you have to do yourself. As an example, the ModuleName module registers its entities to Doctrine like this, together with a configuration option to load the Gedmo tree listener:

return array(
    'di' => array(
        'instance' => array(
            // Set Doctrine annotations in driver chain
            // orm_driver_chain is an alias for DoctrineORMModule\Doctrine\ORM\DriverChain
            'orm_driver_chain' => array(
                'parameters' => array(
                    'drivers' => array(
                        'a_unique_key_here' => array(
                            'class'     => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                            'namespace' => 'ModuleName\Entity',
                            'paths'     => array(__DIR__ . '/../src/ModuleName/Entity')
                        ),
                    ),
                ),
            ),

            // Set Gedmo tree subscriber
            'orm_evm' => array(
                'parameters' => array(
                    'opts' => array(
                        'subscribers' => array('Gedmo\Tree\TreeListener')
                    )
                )
            ),
        ),
    ),
);

Keep in mind to replace ModuleName by your module name and the a_unique_key_here as well (lowercase, underscore separated is generally a good idea).