Integrating Doctrine: Symfony vs Yii

Continuing with Symfony vs Yii series, in this post will be shown how to integrate the best PHP ORM into our project. The versions that will be used are Yii 1.1.5, Symfony 1.4.8, and Doctrine 1.2.3.

First, it will be shown the steps to properly integrate Doctrine with Yii, which has no native connection, but there is a very useful plugin (doctrine-Yii) to do so. Then the same but with Symfony, which will be a very straightforward process because Symfony has native integration with Doctrine.

Yii way

1.- Download Doctrine 1.2.3 (The latest stable release)

Browse the Doctrine Download Page and download the latest release in branch 1.

In the project/protected folder create one named vendors and inside this new folder create another one named doctrine. In that folder extract the doctrine package.

2.- Install the Doctrine-Yii Plugin

Download the Doctrine-Yii plugin from the download page.

There is two classes you have to copy to your project:

DoctrineCommand class

In the plugin site it is describe the DoctrineCommand installation process. It will be describe the most important parts here.

Copy the DoctrineCommand class to project/protected/commands/shell folder. Now, if you enter to the yiic shell utility uou will see a new doctrine command to use.

Yii Shell

DoctrineComponent class

As with DoctrineCommand, DoctrineComponent has a installation page. Here it will be describe the most important parts.

Copy the DoctrineComponent class to project/protected/vendors/doctrine folder.

Now you must change the project/protected/config/main.php file with:

 
  'preload' => array('log', 'doctrine'),
 

and

 
  'components' => array(
    'doctrine' => array(
      'class'            => 'application.vendors.doctrine.DoctrineComponent',
      'connectionString' => 'mysql://username:[email protected]/dbname',
    ),
    ...
  );
 

In the second chunk you indicate to your app the component class to use and the connection string for the database connection.

If you are a Symfony guy then … Yes my friend, there is no Yaml in Yii. just plain php files.

Register Doctrine Autoload method

Open the DoctrineComponent class and update the init method:

 
  public function init()
  {
    parent::init();
 
    spl_autoload_unregister(array('YiiBase','autoload'));
    spl_autoload_register(array('Doctrine','autoload'));
    spl_autoload_register(array('YiiBase','autoload'));
 
    Doctrine_Manager::connection($this->connectionString);
    $modelsPath = Yii::getPathOfAlias("application.models");
    Doctrine::loadModels($modelsPath);
 
    ...
  }
 

You need to unregister the Yii autoload method first, then register the Doctrine autoload method and finally register the Yii method.

To test your doctrine installation you can enter in the yii shell the command: doctrine build-all (you must have at least one table in the schema.yml to build from)

Those are all the steps you need to follow to have Doctrine working with your Yii project.

Remember that the schema.yml and the fixtures.yml files should be located in the project/protected/models folder.

Now you can start updating your db schema and creating initial data for it with Doctrine.

Symfony way

The integration process with Symfony will be much easier because symfony 1.4 is coming by default with Doctrine 1.2.

1.- Activate Doctrine Plugin

Open project/config/ProjectConfiguration class and update the getActivePlugins method:

 
  protected function getActivePlugins()
  {
    return array
           (
             'sfDoctrinePlugin',
             //'symfextPlugin',
           );
  }
 

Once the plugin activated you can use Doctrine ORM Framework. As simple as that.

To test your doctrine activation you can enter the command: symfony doctrine:build –all –and-load –no-confirmation (you must have at least one table in the schema.yml to build from)

Remember that to update your connection properties you must change the project/config/databases.yml file:

 
all:
  doctrine:
    class:         sfDoctrineDatabase
    param:
      dsn:         mysql:host=localhost;dbname=my_database
      username:    root
      password:    ''
      attributes:
        use_dql_callbacks: true
 

 And to add new initial data for the db you can use project/data/fixtures/fixtures.yml file:

In this post it was show how to integrate the best PHP ORM with Symfony and Yii. As you can see it is much easier to integrate with symfony because of its native connection, although it wasn’t that hard to do it with Yii. But remember if you want to work extensively with Doctrine you must consider its level of integration. With Symfony, Doctrine is deeply integrated with the forms framework and it is very easy to set up a new db schema and to generate the form classes. However this deep integration does not exist with Yii, because Yii has its own ORM.

Leave a Reply

Your email address will not be published. Required fields are marked *