Symfony2 and DoctrineExtensions

To add commonly used behaviors to Symfony 2 (eg Doctrine 2) you need to add DoctrineExtentions to your application. DoctrineExtensions adds the following behaviour to Doctrine 2:

  • Tree – this extension automates the tree handling process and adds some tree specific functions on repository. (closure or nestedset)
  • Translatable – gives you a very handy solution for translating records into diferent languages. Easy to setup, easier to use.
  • Sluggable – urlizes your specified fields into single unique slug
  • Timestampable – updates date fields on create, update and even property change.
  • Loggable – helps tracking changes and history of objects, also supports version managment.
  • Sortable – makes any document or entity sortable

Thanks to Stof there is a bundle that you can use that you can find here: https://github.com/stof/StofDoctrineExtensionsBundle. You find instructions on how to install the the bundle as a git submodule there but if you (like me) want to use the vendors functionality instead follow the instructions here instead :).

Add DoctrineExtensions and StofDoctrineExtensionsBundle

Edit the deps file and add the following at the end:

 // deps
...
[gedmo-doctrine-extensions]
   git=http://github.com/l3pp4rd/DoctrineExtensions.git

[Stof-DoctrineExtensionsBundle]
   git=https://github.com/stof/StofDoctrineExtensionsBundle.git
   target=/bundles/Stof/DoctrineExtensionsBundle

then run the following command to install the bundles (and update all your bundles)

./bin/vendors install

Register the DoctrineExtensions and Stof namespaces

// app/autoload.php
...
$loader->registerNamespaces(array(
    ...
    'Stof' => __DIR__ . '/../vendor/bundles',
    'Gedmo' => __DIR__ . '/../vendor/gedmo-doctrine-extensions/lib',
));

Add DoctrineExtensionsBundle to your application kernel

// app/AppKernel.php
...
public function registerBundles()
{
    return array(
        ...
        new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
    );
}

Configure the bundle
Then we need to configure what we need to use. I have the following configuration:

// app/config/config.yml
...
# Doctrine Extensions Configuration
stof_doctrine_extensions:
        default_locale: en_us
        translation_fallback: true
        orm:
            default:
              timestampable: true
              translatable: true

As you can see I am using the translatable and timestampable behavior, the default setting are false (off) so just turn on them as you need them.

To use DoctrineExtentions on a entity add this in top of the class

use Gedmo\Mapping\Annotation as Gedmo;

and instead of:

/**
 * @gedmo:Tree
 */

use

/**
 * @Gedmo\Tree
 */

Examples:
http://0hlsson.se/2011/07/24/symfony2-and-timestampable-example/

For more information about how to set up and use the behaviours see:
https://github.com/stof/StofDoctrineExtensionsBundle/blob/master/Resources/doc/index.rst
https://github.com/l3pp4rd/DoctrineExtensions/tree/master/doc

12 Responses to “Symfony2 and DoctrineExtensions”

  1. r4cker

    Hello,
    thanks for the example, but is it possible to have it with tree.
    I mean
    – example of Entity.php
    – example of controller.php

    thanks 🙂

  2. ravindra khokharia

    Hi,

    First of all thank you for the tutorial, i have implemented the same successfully, but currently the value for only english “en” language is added, Please show me some way through which i can add data for different languages too when adding a new record. i.e when i save my form, records for all the language should be added.

    Waiting eagerly for the solution.

    Thanking you..
    Ravindra

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>