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 http://www.doctrine-project.org/downloads/Doctrine-1.2.3.tgz
[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://user:pass@db.hostname.com/databasename"

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.

Installing and setting up Zend Framework on a Virtual Host

0. Full documentation here:

http://framework.zend.com/manual/en/learning.quickstart.create-project.html

1. Log onto your account. You’ll most like be in your home directory. You should know which directory is your public directory (Web Root). Usually this is something like ‘www‘, ‘public_html‘, or ‘html‘, or maybe even ‘your-domain.com‘. Regardless, you don’t need to be there yet. In the follow examples, anytime you see the directory ‘www‘, assume it’s the Web Root and replace it with whatever your Web Root is.

Also, in the below examples, I’ll use ‘/path/to’. For me, this is ‘/home/jason‘ For you, it might be ‘/home/username‘, or ‘/home/website.com‘. Whatever it is, it’s whatever comes before the rest of the paths. I’ll use ‘/path/to‘ below, just keep in mind, this is different for each person.

If your not sure what this is, when you first log in via SSH, run this command

[server]$ pwd

It will print out the directory your currently in. This is most likely the directory path you’ll want to use for ‘/path/to‘.

[server]$ mkdir src
[server]$ mkdir lib
[server]$ cd src

2. Download and ‘install’ the source

[server]$ wget http://framework.zend.com/releases/ZendFramework-1.10.8/ZendFramework-1.10.8.tar.gz
[server]$ tar xfz ZendFramework-1.10.8.tar.gz
[server]$ mv ZendFramework-1.10.8 ../lib
[server]$ cd ../lib
[server]$ ln -s ZendFramework-1.10.8/ Zend
[server]$ alias zf=/path/to/lib/Zend/bin/zf.sh

You can also place the zf alias in your .bash_profile in your home directory by doing the following

[server]$ cd ~
[server]$ vi .bash_profile

You can also use nano, or any other editor you are familiar with, and add this line

alias zf=/path/to/lib/Zend/bin/zf.sh

After all this is done, you’ve essentially installed Zend Framework on your virtual host. However, now you need to set it up for your site.

3. Creating your project

[server]$ cd ~
[server]$ zf create project PROJECTNAME

If you have an existing Web Root, you’ll want to move this or remove it, or change it. Basically, at this point, we want the web root pointing to the correct directory in the Zend Project we just created.

If my Web Root was a directory named ‘www’, I’d do this:

[server]$ mv www www_old

Keep in mind by doing this, I can no longer access my website hosted in that directory. We’ll set this up now.

[server]$ ln -s PROJECTNAME/public/ www

In this case, ‘www’ should be the name of your Web Root.

Now we need to need to make sure the site can find the Zend Framework. Remember, it’s in the lib directory.

[server]$ ln -s /path/to/lib/Zend/library/Zend .

At this point, you should be able to go to your website and check out the default web page. Congrats!

Also, log out, and then log back in. If you entered the alias for your .bash_profile, you should be able to run this:

[server]$ zf show version
Zend Framework Version: 1.10.8

Now you can use the zf script helper functions, and proceed with a working Zend Framework setup.

[server]$ zf help

This will also work.