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

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
Install
$ bin/vendors install
Add the next lines to the app/autoload.php file:
$loader->registerNamespaces(array(
    // ...
    'Stof'  => __DIR__.'/../vendor/bundles',
    'Gedmo' => __DIR__.'/../vendor/gedmo-doctrine-extensions/lib',
    // ...
));
And the following bundle to app/AppKernel.php:
public function registerBundles()
{
    return array(
        // ...
        new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
        // ...
    );
}
Once done, configure it at app/config/config.yml (add the listeners, more info and options at StofDoctrineExtensions)
#...
doctrine:
    orm:
        auto_mapping: true

stof_doctrine_extensions:
    orm:
        default:
            sluggable: true
#...
Do not forget to clear the cache
$ 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;
    }
}
You can get more examples and info going to the next links: Sluggable, Translatable, Timestampable, Loggable and Tree.
Via: Symfony
More information: 
More news about:  Symfony2
Tags:  php,  Symfony2,  Sluggable,  Translatable,  Timestampable,  Loggable,  Tree,  Doctrine2

Comments

  • Gravatar
    08.01.12 - 05:08  SlowProg

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

Comment