jurian sluiman

{
Wissel naar

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).

 

Comments

Mike

You still however, have to setup the listeners for the Doctrine extensions by setting the subscriber option for orm_evm.

Jurian Sluiman

That's true Mike. The orm_evm configuration wasn't something covered by my module, but you needed that per module to get it working. However, I documented the configuration keys in the README (which I replaced completely) so I added the example in this post above. Thanks for the notification :)

mludewig

Hey @ all,
i tried to configure the gedmo timestampable last Weekend but had some problems after running doctrine orm:schema-tool:create after all. I installed all with the composer (ZF2 B4, doctrineORM, gedmo ) and the skeleton is running correct. The fields for created and update are available in DB (innodb), but the flag for CURRENT_TIMESTAMP and ON_UPDATE_CURRENT_TIMESTAMP is not set. I configured all like the posts here but it will not work correct like it should :) Hope you can help me with my mistake in configuration.

Best regards.
Matthias

Jurian Sluiman

Matthias, what I know is you need to

1) register Gedmo namespace
2) inject the appropriate listeners
3) use the right annotations

Do you use the latest Doctrine module? Last week I saw the configuration changed (so the part from "Update (configuration)" of this blog. I will update the code soon, but you can already have a look at a module I have with a tree listener: https://github.com/juriansluiman/SlmCmfKernelDoctrineOrm/blob/master/config/module.config.php#L7

As far as I can see from the Gedmo documentation (https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/timestampable.md) it's should "just work" with these steps. If you think there's something wrong on your Gedmo configuration, you might try to ask help from Gediminas Morkevičius as well, as he designed the Gedmo extensions. And if you have problems with the Doctrine configuration, you can perhaps ask Kyle Spraggs too (SpiffyJr), maintainer of the DoctrineModule for the Zend Framework.

If you still have problems, just let me know!

Joseph Radtke

Doctrine doesn't appear to ahve a setter for "opts" or "paramters".

What worked for me is the following:

'eventmanager'=>array(
'orm_default'=>array(
'subscribers' =>array('Gedmo\Sluggable\SluggableListener'),

),
),

Place a comment

If you have a user account for this site, you can login to click here.

Please note your comment below will be removed if you login!

 
 

The address is stored internally but not displayed on this site. We will respect your privacy.

In your message no html is allowd. A blank line creates a new paragraph, an url gets a hyperlink.