Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Lithium integration with Doctrine2
tree: 623f2ae1e5

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.

li3_doctrine2 offers integration between the most RAD PHP framework and possibly the best PHP 5.3 ORM out there: Doctrine2


li3_doctrine2 is released under the BSD License.


It is recommended that you install li3_doctrine2 as a GIT submodule, in order to keep up with the latest upgrades. To do so, switch to the core directory holding your lithium application, and do:

$ git submodule add app/libraries/li3_doctrine2
$ cd app/libraries/li3_doctrine2 && git submodule update --init
$ cd _source/doctrine2 && git submodule update --init


Adding the li3_doctrine2 library

Once you have downloaded li3_doctrine2 and placed it in your app/libraries, or your main libraries folder, you need to enable it by placing the following at the end of your app/config/bootstrap/libraries.php file:


Defining a connection

Setting up a connection with li3_doctrine2 is easy. All you need to do is add the following to your app/config/bootstrap/connections.php file (make sure to edit the settings to match your host, without altering the type setting):

Connections::add('default', array(
    'type' => 'Doctrine',
    'driver' => 'pdo_mysql',
    'host' => 'localhost',
    'user' => 'root',
    'password' => 'password',
    'dbname' => 'my_db'

Working with models

Creating models

When looking to create your doctrine models, you have two choices: you can have them follow your custom class hierarchy (or not at all), or you could have them extend from the BaseEntity class provided by this library. The advantage of choosing the later is that your models will have lithium's validation support, and can be better integrated with the custom adapters (such as for session management, or for authorization) provided by this library.

Let us create a User model. Following doctrine's basic mapping guide we'll use annotations to define the properties, and we will also include lithium validation rules (that's why we are choosing to extend this model from BaseEntity):

namespace app\models;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Table;
use lithium\security\Password;

 * @Entity
 * @Table(name="users")
class User extends \li3_doctrine2\models\BaseEntity {
     * @Id
     * @GeneratedValue
     * @Column(type="integer")
    protected $id;

     * @Column(type="string",unique=true)
    protected $email;

     * @Column(type="text")
    protected $password;

     * @Column(type="string")
    protected $name;

     * Validation rules
    protected $validates = array(
        'email' => array(
            'required' => array('notEmpty', 'message' => 'Email is required'),
            'valid' => array('email', 'message' => 'You must specify a valid email address', 'skipEmpty' => true)
        'password' => array('notEmpty', 'message' => 'Password must not be blank'),
        'name' => array('notEmpty', 'message' => 'Please provide your full name')


    public function getId() {
        return $this->id;

    public function getEmail() {
        return $this->email;

    public function setEmail($email) {
        $this->email = $email;

    public function setPassword($password) {
        $this->password = !empty($password) ? Password::hash($password) : null;

    public function getName() {
        return $this->name;

    public function setName($name) {
        $this->name = $name;

Using the Doctrine shell to Generate the schema

Once you have your model(s) created, you can use doctrine's shell to generate the schema. li3_doctrine2 offers a wrapper for doctrine's shell that reutilizes lithium's connection. To run the shell stand in the core directory if your application and do:

$ app/libraries/li3_doctrine2/bin/doctrine

That will give you all the available commands. For example, to get the SQL you should run to create the schema for your models, do:

$ app/libraries/li3_doctrine2/bin/doctrine orm:schema-tool:create --dump-sql

which will give an output similar to the following:

    email VARCHAR(255) NOT NULL, 
    password LONGTEXT NOT NULL, 
    name VARCHAR(255) NOT NULL, 
    UNIQUE INDEX UNIQ_1483A5E9E7927C74 (email), 

Integrating Doctrine libraries

In this section I'll cover some of the doctrine extension libraries out there, and how to integrate them with li3_\doctrine2.


If there is one tool I would recommend you checkout for your Doctrine models, that would be DoctrineExtensions. It provides with a set of behavioral extensions to the Doctrine core that will simplify your development.

To use DoctrineExtensions, you should first add it as GIT submodule. To do so, switch to the core directory holding your lithium application, and do:

$ git submodule add app/libraries/_source/DoctrineExtensions

Next you would use your connection configuration (in app/config/connections.php) to configure Doctrine with your desired behaviors. For example, if you wish to use Timestampable and Sluggable, you would first add the library in app/config/libraries.php:

Libraries::add('Gedmo', array(
    'path' => LITHIUM_APP_PATH . '/libraries/_source/DoctrineExtensions/lib/Gedmo'

And then you would filter the createEntityManager method in the Doctrine datasource to add the behaviors. Edit your app/config/connections.php file and add the following right below the connection definition:

    function($self, $params, $chain) {
            new \Gedmo\Timestampable\TimestampableListener()
            new \Gedmo\Sluggable\SluggableListener()
        return $chain->next($self, $params, $chain);
Something went wrong with that request. Please try again.