Bootstrapping Doctrine 1.2 and Zend 1.10/1.11 the easy way

So there seem to be many different ways to get the 1.2 version of Doctrine running with the Zend Framework (currently 1.11) but the easiest way that worked for me, after using the Zend framework command line tool to create a new standard project, made my application bootstrap file look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
< ?php
 
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected function _initDoctrine()
	{
		$manager = Doctrine_Manager::getInstance();
		$options = $this->getOption("doctrine");
		if(empty($options['models_path']))
			$options['models_path'] =  APPLICATION_PATH . '/models';
 
		foreach($options as $key => $value)
		{
			if($key != 'connection')
				$manager->setAttribute($key, $value);
		}
 
		if(empty($options['connection']))
			throw new Exception("No database connection string provided!");
 
		Doctrine_Manager::connection($options['connection']);
		Doctrine::loadModels($options['models_path']);
		return $manager;
	}
}
?>

The application.ini configuration file then needs some of these information:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
; Database configuration
doctrine.connection = "mysql://user:pass@localhost/database"
doctrine.models_path = APPLICATION_PATH "/models"
doctrine.data_fixtures_path = APPLICATION_PATH . "/data/fixtures"  
doctrine.migrations_path = APPLICATION_PATH . "/data/migrations"  
doctrine.sql_path = APPLICATION_PATH . "/data/sql"
doctrine.yaml_schema_path = APPLICATION_PATH . "/data/schema
; Options for generating models from YAML
doctrine.generate_models_options.phpDocPackage = "Your PHP doc package"
doctrine.generate_models_options.phpDocSubpackage = "Subpackage"  
doctrine.generate_models_options.baseClassName = "Model_"  
doctrine.generate_models_options.phpDocName = "Your name"  
doctrine.generate_models_options.phpDocEmail = "email@address.com"  
; Set this to true to generate base classes. Set to false to make autoloading easier
doctrine.generate_models_options.generateBaseClasses = false

The command line interface is a good way of dealing with model and database generation, this is how the PHP CLI script for Doctrin + Zend looks like (inspired by this post):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/php
< ?php
/**
 * Doctrine CLI script
 */
 
define('APPLICATION_ENV', 'development');
 
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
 
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));
 
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');
 
$config = $application->getOption('doctrine');
 
$cli = new Doctrine_Cli($config);
$cli->run($_SERVER['argv']);
?>

With this you can get started to use Doctrine + Zend Framework.

About Daff

I started studying computer sciences at the Munich university of applied sciences in late 2005.
This entry was posted in common and tagged , , . Bookmark the permalink.

One Response to Bootstrapping Doctrine 1.2 and Zend 1.10/1.11 the easy way

  1. Alelasantillan says:

    I did what you suggested:
    0. I created a new zf project using #zf create project test11. Changed the Bootstrap.php file by the code you posted2. Added lines to the test1/application/configs/application.ini file, using the right settings for the db connector3. I created the test1/bin directory and inside I put the cli doctrine.php scriptFinally I put the contents of the lib directory that come into the Doctrine-1.2.2 package into the test1/library file

    Then, the test failed:

    php public/index.php PHP Fatal error:  Uncaught exception ‘Zend_Config_Exception’ with message ‘syntax error, unexpected $end, expecting TC_DOLLAR_CURLY or TC_QUOTED_STRING or ‘”‘ in /usr/local/zend/apache2/htdocs/alela/prueba/application/configs/application.ini on line 39’ in /usr/local/zend/share/ZendFramework/library/Zend/Config/Ini.php:182Stack trace:#0 /usr/local/zend/share/ZendFramework/library/Zend/Config/Ini.php(202): Zend_Config_Ini->_parseIniFile(‘/usr/local/zend…’)#1 /usr/local/zend/share/ZendFramework/library/Zend/Config/Ini.php(126): Zend_Config_Ini->_loadIniFile(‘/usr/local/zend…’)#2 /usr/local/zend/share/ZendFramework/library/Zend/Application.php(386): Zend_Config_Ini->__construct(‘/usr/local/zend…’, ‘production’)#3 /usr/local/zend/share/ZendFramework/library/Zend/Application.php(85): Zend_Application->_loadConfig(‘/usr/local/zend…’)#4 /usr/local/zend/apache2/htdocs/alela/prueba/public/index.php(24): Zend_Application->__construct(‘production’, ‘/usr/local/zend…’)#5 {main}  thrown in /usr/local/zend/share/ZendFramework/library/Zend/Config/Ini.php on line 182

    any idea?

Leave a Reply