1

Topic: Bootstrapping Doctrine in Zend 1.8 or 1.9

I've been using Doctrine a lot as an alternative to Zend_Db with great success. here's my bootstrap method (sits in the Bootstrap.php file) in case anyone is interested:


PHP Code:
 
    protected function _initDoctrine()
    {
        $this->bootstrap("autoload");
 
        $dbConfig = $this->options['db'];
        defined('CONFIG_PATH')             || define('CONFIG_PATH', APPLICATION_PATH . '/configs');    
        defined('DATA_FIXTURES_PATH')     || define('DATA_FIXTURES_PATH',     CONFIG_PATH . '/data/fixtures');
        defined('SQL_PATH')             || define('SQL_PATH',                 CONFIG_PATH . '/data/sql');
        defined('MIGRATIONS_PATH')         || define('MIGRATIONS_PATH',         CONFIG_PATH . '/migrations');
        defined('YAML_SCHEMA_PATH')     || define('YAML_SCHEMA_PATH',         CONFIG_PATH . '/schema.yml');
        defined('MODELS_PATH')             || define('MODELS_PATH',             APPLICATION_PATH . '/models');
        defined('DB_PATH')                 || define('DB_PATH' ,                 'mysql://' . $dbConfig['username']. ':' . $dbConfig['password']. '@' . $dbConfig['host']. '/' . $dbConfig['name']);
 
        require_once 'Doctrine.php';
        
        spl_autoload_register(array('Doctrine', 'autoload'));
 
        $connection = Doctrine_Manager::connection(DB_PATH);
        $connection->setCharset('UTF8');
        
        Doctrine_Manager::getInstance()->setAttribute('model_loading', 'conservative');
        Doctrine::loadModels(MODELS_PATH);
    }
 

this code assumes that the database configurin is sitting in your application.ini file. Also, Doctrine is sitting in /library/Doctrine. Thanks to Maxime Bouroumeau, I've also got a modified version of the Doctrine.php command line script:

PHP Code:
 
<?php
error_reporting(E_ALL);
 
define('ROOT_PATH', dirname(dirname(dirname(__FILE__))));
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/..'));
define('APPLICATION_ENV', 'development');
define('TMP_PATH', realpath(dirname(__FILE__) . '/../tmp/'));
 
 
//Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));
 
/** Zend_Application */
require_once 'Zend/Application.php';  
 
// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV, 
    APPLICATION_PATH . '/configs/application.ini'
);
 
$application->getBootstrap()->bootstrap("doctrine");
 
// Configure Doctrine Cli
// Normally these are arguments to the cli tasks but if they are set here the arguments will be auto-filled
$config = array('data_fixtures_path'  =>  DATA_FIXTURES_PATH,
                'models_path'         =>  MODELS_PATH,
                'migrations_path'     =>  MIGRATIONS_PATH,
                'sql_path'            =>  SQL_PATH,
                'yaml_schema_path'    =>  YAML_SCHEMA_PATH);
 
 
$cli = new Doctrine_Cli($config);
$cli->run($_SERVER['argv']);
 

Hope this helps other integrators!

2

Re: Bootstrapping Doctrine in Zend 1.8 or 1.9

Hi Jon,

your article was indeed very helpful. Doctrine works like a charm now.

I made some improvements/changes to your sourcecode however... maybe you find them useful.

Method in the Bootstrap class:

PHP Code:
 
protected function _initDoctrine()
    {
       // get options from application.ini rather than defining all paths in the bootstrap class again
        $options = $this->getOptions();
        
        if ( !isset( $options['doctrine']['connection_string'] ) ) {
            // doctrine not needed at all...
            return ;
        }
                
        require_once 'Doctrine/Doctrine.php';
 
        spl_autoload_register(array('Doctrine', 'autoload'));
        
        $manager = Doctrine_Manager::getInstance();
                
        $connection = $manager->connection( $options['doctrine']['connection_string'] );
        $connection->setCharset('UTF8');
        
        $manager->setAttribute( Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_CONSERVATIVE );
        
        // if models_path is not set, assume it's in application/models
        $modelPath = isset( $options['doctrine']['models_path']  ) ? $options['doctrine']['models_path'] : APPLICATION_PATH.DIRECTORY_SEPARATOR.'models';
        Doctrine::loadModels( $modelPath );
    }
 

... and my doctrine_cli.php:

PHP Code:
 
error_reporting(E_ALL);
 
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', dirname(realpath(dirname(__FILE__))).DIRECTORY_SEPARATOR.'application');
 
define('DOCTRINE_PATH', APPLICATION_PATH.DIRECTORY_SEPARATOR.'doctrine');
 
// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
 
//Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));
 
/** Zend_Application */
require_once 'Zend/Application.php';  
 
// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV, 
    APPLICATION_PATH . '/configs/application.ini'
);
 
$application->getBootstrap()->bootstrap("doctrine");
 
// Configure Doctrine Cli
// Normally these are arguments to the cli tasks but if they are set here the arguments will be auto-filled
$options = $application->getOptions();
$config = array(
            'models_path'         => isset( $options['doctrine']['models_path'] ) ? $options['doctrine']['models_path'] : APPLICATION_PATH.DIRECTORY_SEPARATOR.'models',
            'data_fixtures_path'  => isset( $options['doctrine']['data_fixtures_path'] ) ? $options['doctrine']['data_fixtures_path'] : DOCTRINE_PATH.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'fixtures',
            'sql_path'            => isset( $options['doctrine']['sql_path'] ) ? $options['doctrine']['sql_path'] : DOCTRINE_PATH.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'sql',
            'migrations_path'     => isset( $options['doctrine']['migrations_path'] ) ? $options['doctrine']['migrations_path'] : DOCTRINE_PATH.DIRECTORY_SEPARATOR.'migrations',
            'yaml_schema_path'    => isset( $options['doctrine']['yaml_schema_path'] ) ? $options['doctrine']['yaml_schema_path'] : DOCTRINE_PATH.DIRECTORY_SEPARATOR.'schema'
);
 
$cli = new Doctrine_Cli( $config );
$cli->run( $_SERVER['argv'] );
 

Pros:
- doctrine settings only need to be defined in application.ini
- only "connection_string" setting is mandatory

Note: I'm only getting started with my first Zend/Doctrine project so this might not be the "expert solution".

Last edited by chmuul (2009-12-30 17:18:33)