Setting up Doctrine with Zend Framework

So you have set up doctrine and put it in your servers php library include path. You’ll need this to be in your applications library folder for when you upload it, but rather than copy it in wasting disk space, we’ll symlink it:

ln -s /usr/lib/php/Doctrine ~/Sites/your-site/library/Doctrine

Now lets configure Zend. Open application.ini and bung this stuff in:

autoloaderNamespaces[] = "Doctrine_"
resources.db.adapter = PDO_MYSQL
doctrine.dsn = "mysql://USER:PASS@localhost/DATABASE"

This lets it know about autoloading classes, the type of database adapter we use, and the connection settings. (Be sure to replace USER PASS & DATABASE with your credentials).

Next we initialise Doctrine in the bootstrap:

public function _initDoctrine()
    $manager = Doctrine_Manager::getInstance();
    $manager->setAttribute(    Doctrine_Core::ATTR_MODEL_LOADING,        Doctrine_Core::MODEL_LOADING_CONSERVATIVE);
    $config = $this->getOption('doctrine');
    $conn = Doctrine_Manager::connection($config['dsn'],'doctrine');
    return $conn;

If all is going well then we should be able to test it out and get a list of databases on the server. In the controller and action of your choosing, try the following out:

// get doctrine manager
 $manager = Doctrine_Manager::getInstance();     
// get connection
$conn = Doctrine_Manager::connection( 'mysql://root@localhost/bzf:[123456]', 'doctrine');
// get databases
$this->view->databases = $conn->import->listDatabases();

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s