Doctrine 2 requires PHP 5.3 so you must have this version in order to follow and test this article.

Today I am happy to tell you that this version of sfDoctrinePlugin is now available and ready for you to use. This article will give you a little information about how you can get started using it today!

Installing

First we need to install the plugin from SVN with the following command from the root of your project:

$ svn co http://svn.symfony-project.org/plugins/sfDoctrinePlugin/branches/1.3-2.0/ plugins/sfDoctrine2Plugin

Now you just need to enable the plugin:

class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    $this->enablePlugins('sfDoctrine2Plugin');
  }
}

Configuring Database Connections

Open your config/databases.yml and configure it for your database connection:

all:
  doctrine:
    class: sfDoctrineDatabase
    param:
      options:
        driver: pdo_mysql
        user: root
        password:
        dbname: doctrine

The DSN(data source name) no longer exists in Doctrine 2. Connection information is much simpler and just supplied as an array of information. You are required to at least specify a driver and the remaining options that are available and required are determined by the driver.

Configuring Your Schema

You can configure your schema much in the same way you always have in config/doctrine. The syntax of the schema files are much different now though. Below is an example of a simple User entity:

# config/doctrine/schema.yml
 
Models\User:
  type: entity
  table: user
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    username:
      type: string
      length: 255
    password:
      type: string
      length: 255

Writing Data Fixtures

The times of using YAML for data fixtures is no longer. Instead, you are only required to use plain PHP for loading your data fixtures.

// data/fixtures/fixtures.php
 
$em = $this->getEntityManager();
 
$admin = new \Models\User();
$admin->username = 'admin';
$admin->password = 'changeme';

Building Doctrine

Now you're ready to build everything. The following command will build models, forms, filters, database and load data fixtures.

$ php symfony doctrine:build --all --and-load

Updating Schema

If you change your schema mapping information and want to update the database you can easily do so by running the following command after changing your mapping information.

$ php symfony doctrine:build --all-classes --and-update-schema

Doctrine 2 In Action

So now that you have everything setup and running lets explore some of the way Doctrine 2 works and how it is integrated with Symfony.

Custom Repository Class

The EntityRepository class is basically what Doctrine_Table is in Doctrine 1. Instead of it being magical and automatic, you simply need to configure your entity mapping information to use a repository class.

Models\User:
  type: entity
  table: user
  repositoryClass: UserRepository
# ...

Now define a UserRepository class somewhere that Symfony can autoload it.

class UserRepository extends EntityRepository
{
  public function getActiveUsers()
  {
    $qb = $this->createQueryBuilder('u');
    $q = $qb->getQuery();
 
    return $q->execute();
  }
}

Now you can use this method like the following:

public function executeIndex()
{
  // ...
 
  $repository = $em->getRepository('Models\User');
  $users = $repository->getActiveUsers();
}

When using Doctrine 2 with Symfony, our entities extend a base class so we have some additional possibilities. You can access the repository methods via static calls, similar to how it is done in Ruby on Rails Active Record.

$users = \Models\User::getActiveUsers();

You also have the same type of built in finders as you do in Doctrine 1.

$user = \Models\User::find(1);
$user = \Models\User::findOneByUsername('jwage');

Entity Manager from Actions

When you are in a Symfony action you can retrieve an entity manager instance by using the getEntityManager() method.

public function executeIndex()
{
  $em = $this->getEntityManager();
 
  $user = new \Models\User();
  $user->username = 'jwage';
  $user->password = 'changeme';
  $user->save();
 
  $em->flush();
}

By default the method returns the entity manager for the last configured database inside databases.yml. You can optionally give an argument for which entity manager to get.

public function executeIndex()
{
  $em = $this->getEntityManager('conn_name');
 
  // ...
}

That is all for now!

This article can't possibly explain everything new about Doctrine 2 so it is recommended that you start reading the documentation on Doctrine 2 from the Doctrine website.