28.09.11 - Sluggable, Timestampable, etc. in Symfony 2
Doctrine 2 behaviours under Symfony 2: Sluggable, Translatable, Timestampable, Loggable and Tree
In Symfony 2, we can attach this useful behaviours to our model through doctrine extensions. One bundle, DoctrineExtensionsBundle, offers a lot of them: Sluggable, Translatable, Timestampable, Loggable y Tree.
The above extensions allow, among other things, to transform usual strings into friendly ones, great for URLs (Sluggable), database i18n support (Translatable), entities tracking and versioning (Loggable), create and update datetime autofill fields (Timestampable) and native database tree structure (Tree).
Install
Add the next lines to the app/autoload.php file:
And the following bundle to app/AppKernel.php:
Once done, configure it at app/config/config.yml (add the listeners, more info and options at StofDoctrineExtensions)
Do not forget to clear the cache
You can get more examples and info going to the next links: Sluggable, Translatable, Timestampable, Loggable and Tree.
The above extensions allow, among other things, to transform usual strings into friendly ones, great for URLs (Sluggable), database i18n support (Translatable), entities tracking and versioning (Loggable), create and update datetime autofill fields (Timestampable) and native database tree structure (Tree).
Installation
At first, we have to install Doctrine extensions and its Symfony Bundle integration. Add the next 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
git=git://github.com/l3pp4rd/DoctrineExtensions.git
[DoctrineExtensionsBundle]
git=git://github.com/stof/StofDoctrineExtensionsBundle.git
target=/bundles/Stof/DoctrineExtensionsBundle
$ bin/vendors install
$loader->registerNamespaces(array(
// ...
'Stof' => __DIR__.'/../vendor/bundles',
'Gedmo' => __DIR__.'/../vendor/gedmo-doctrine-extensions/lib',
// ...
));
// ...
'Stof' => __DIR__.'/../vendor/bundles',
'Gedmo' => __DIR__.'/../vendor/gedmo-doctrine-extensions/lib',
// ...
));
public function registerBundles()
{
return array(
// ...
new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
// ...
);
}
{
return array(
// ...
new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
// ...
);
}
#...
doctrine:
orm:
auto_mapping: true
stof_doctrine_extensions:
orm:
default:
sluggable: true
#...
doctrine:
orm:
auto_mapping: true
stof_doctrine_extensions:
orm:
default:
sluggable: true
#...
$ php app/console cache:clear
Usage example
One example entity with the Sluggable and Timestampable behaviours activated could be:<?php
namespace Entity;
use Gedmo\Mapping\Annotation as 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;
public function getId()
{
return $this->id;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
public function getSlug()
{
return $this->slug;
}
public function getCreated()
{
return $this->created;
}
public function getUpdated()
{
return $this->updated;
}
}
namespace Entity;
use Gedmo\Mapping\Annotation as 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;
public function getId()
{
return $this->id;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
public function getSlug()
{
return $this->slug;
}
public function getCreated()
{
return $this->created;
}
public function getUpdated()
{
return $this->updated;
}
}

Excellent! That's what I was looking for. A very handy thing. Thank you very much!