Installing and setting up Doctrine ORM alongside Zend Framework


Picking up from my previous article on setting up Zend Framework, let's start with at the beginning and go grab the latest Doctrine files.

[server]$ cd ~[server]$ cd src/[server]$ wget[server]$ tar xfz Doctrine-1.2.3.tgz[server]$ mv Doctrine-1.2.3 ../lib/[server]$ cd ../lib/[server]$ ln -s Doctrine-1.2.3/ Doctrine[server]$ cd ../ZENDPROJECT/library/[server]$ ln -s ../../lib/Doctrine/Doctrine[server]$ ln -s ../../lib/Doctrine/Doctrine.php

Pretty simple and straightforward, and mostly setting things so Doctrine will sit alongside Zend.  Remember to replace ZENDPROJECT above with the name of your Zend Project directory.

After this, you want to open your application.ini file for Zend, and add this line:

autoloaderNamespaces[] = "Doctrine"

You also want to generate your DSN for your database connection now as well.  So, add this to the next line:

dsn = "database://"

Save the file.

Now, we need to open up the Bootstrap.php file and create the following function:

protected function _initDoctrine(){    $manager = Doctrine_Manager::getInstance();    $dsn = $this->getOption('dsn');    $conn = $manager->connection($dsn, 'doctrine');    return $conn; }

This is the bare minimum you'll need.  You can actually get by with less, but we are grabbing the DSN from the application.ini we set before.  Matthew has a more complete blog post on the topic, and shares his entire _initDoctrine method with you if you want to initialize your Doctrine instance with additional options set.

From here, you can go ahead and generate your models in application/models and extend them from Doctrine_Record like normal.