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
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 🙂
Hi!
Might put that up later on. At the moment I am working with another project (in yii) so it might be a while.
Hi,
I found example in this forum :
It may help
http://www.developpez.net/forums/d1123627/php/bibliotheques-frameworks/symfony2/champs-vides-ne-peuvent-letre-sauf/
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
When light rays arе redirected by the phaкіc lens onto the retina, the
nearsighted eye sеes distant objects a lot more clearly.
My web blog ⅼen mat (lensmathanquoc.wordpress.com)