⬆️ ⬇️

Sluggable, Timestampable, and so on in Symfony 2

In the second symfony, we can connect the behaviors to our models using Doctrine extensions. One bundle, StofDoctrineExtensionsBundle , makes it possible to use the following behaviors: Sluggable , Translatable , Timestampable , Loggable, and Tree .



The above extensions allow you to turn ordinary links into readable (sluggable), perform simple internationalization support (translatable), organize version control (loggable), create and automatically fill in the creation date and update date (timestampable), and organize a tree-like data storage structure (tree).



Installation



The first step is to install the Doctrine extensions and its symfony bundle. Add the following lines to the deps file:



[gedmo-doctrine-extensions] git=git://github.com/l3pp4rd/DoctrineExtensions.git [DoctrineExtensionsBundle] git=git://github.com/stof/StofDoctrineExtensionsBundle.git target=/bundles/Stof/DoctrineExtensionsBundle 


Installation

')

 $ bin/vendors install 


If any problems arise with the installation of the vendors, simply delete everything from the vendors directory and try again.



Now add the following lines to app / autoload.php :



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


Also add the installed package to app / AppKernel.php :



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


After that, add to the app / config / config.yml configuration (add listeners, additional information about StofDoctrineExtensions available listeners):



 #... doctrine: orm: auto_mapping: true stof_doctrine_extensions: orm: default: sluggable: true timestampable: true #... 


Now, do not forget to clean the cache:



 $ php app/console cache:clear 


Using



Let's consider using Slugable and Timestampable as examples.



 <?php namespace Entity; use Gedmo\Mapping\Annotation as Gedmo; //  Gedmo use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table(name="article") * @ORM\Entity */ class Article { /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(length=64) */ private $title; /** * @Gedmo\Slug(fields={"title"}) * @ORM\Column(length=128, unique=true) */ private $slug; /** * @var datetime $created * * @Gedmo\Timestampable(on="create") * @ORM\Column(type="date") */ private $created; /** * @var datetime $updated * * @ORM\Column(type="datetime") * @Gedmo\Timestampable(on="update") */ private $updated; 


Doctrine itself will generate the rest for you:



 $ app/console doctrine:generate:entities 


You can also see other examples of using Sluggable , Translatable , Timestampable , Loggable and Tree .



Translator's Note



Translation is not word for word, but the meaning is fully conveyed. Also in the article there are some ad-libbing, but it seems to me, extremely useful.



Read the same: Combined forms and default values .

Source: https://habr.com/ru/post/134258/



All Articles