Setting Up Doctrine for Zend Framework 1.9.x

After some fid­dling and googling and Zend­cast watch­ing :) I fig­ured out how to get the mod­els to gen­er­ate the way I needed them to.
And fig­ur­ing out why generate-sql and build-all-reload did not cre­ate sql schema nor cre­ate any tables in the mysql database.

Direc­tory Structure

Stan­dard ZF direc­tory struc­ture with a few addi­tion (Can’t wait for ZFTool to do all this for us)

├───application
│   ├───configs
│   │   ├───data          < --
│   │   │   ├───fixtures <--
│   │   │   └───sql        <--
│   │   └───migrations  <--
│   ├───controllers
│   ├───models
│   ├───scripts            <-- doctrine.php & bat here
│   └───views
├───library
│   ├───Doctrine        <-- Please use the latest 1.2.x
│   ├───vendor         <-- Do not forget this one or things will be bad.
├───public

 

application.ini

Only applic­a­ble parts added.
[production]
phpSettings.date.timezone = "Europe/Stockholm"
autoloaderNamespaces[] = "Doctrine"
doctrine.dsn = "mysql://root@localhost/testbench"
doctrine.data_fixtures_path = APPLICATION_PATH "/configs/data/fixtures"
doctrine.sql_path = APPLICATION_PATH "/configs/data/sql"
doctrine.migrations_path = APPLICATION_PATH "/configs/migrations"
doctrine.yaml_schema_path = APPLICATION_PATH "/configs/schema.yml"
doctrine.models_path = APPLICATION_PATH "/models"

Bootstrap.php

Please note the inline com­ment about model load­ing attrib­utes, this is what broke it for me, for some rea­son using con­ser­v­a­tive will pre­vent gen­er­a­tion and cre­ation of data­base tables and schemas, although model gen­er­a­tion works fine, very puzzling!.

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

pro­tected func­tion _initDoctrine() {
$this->getApplication()
->getAu­toloader()
->pushAu­toloader ( array (‘Doc­trine’, ‘autoload’ ) );
//spl_autoload_register ( array (‘Doc­trine’, ‘mod­el­sAutoload’ ) );
$man­ager = Doctrine_Manager::getInstance ();
$manager->setAttribute ( Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true );
// The Model Load­ing acts a tad weird — Use Default for now.
//$manager->setAttribute ( Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_AGGRESIVE); // MODEL_LOADING_CONSERVATIVE
$manager->setAttribute ( Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, false );

$doc­trineCon­fig = $this->getOption(‘doctrine’);
$conn = Doctrine_Manager::connection($doctrineConfig[’dsn’],‘doctrine’);
$conn->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM,true);

}
}

doctrine.bat

Since this dev machine is on Win­dows XP:
@echo off
echo Running Doctrine CLI.
"C:\Zend\ZendServer\bin\php.exe" -f C:\Zend\Apache2\htdocs\testbench\application\scripts\doctrine.php %1 %2 %3 %4 %5 %6 %7 %8 %9

doc­trine

If you are linux/mac based: (Dont for­get to chmod +x it)

#!/usr/bin/env php
< ?php
chdir(dirname(__FILE__));
include('doctrine.php');

doctrine.php

Please note the inline com­ments in this one and cus­tomize it to your own lik­ing.

< ?php
/**
* Doctrine CLI
*/
error_reporting(E_ALL);

define(‘ROOT_PATH’, realpath(dirname(__FILE__)));
define(‘APPLICATION_PATH’, realpath(dirname(__FILE__) . “/../”));
define(‘APPLICATION_ENV’, ‘development’);

//Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
‘../library’,get_include_path() )));
/** Zend_Application */
require_once ‘Zend/Application.php’;

// Cre­ate appli­ca­tion, boot­strap, and run
$appli­ca­tion = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . ‘/configs/application.ini’
);

// Read in the application.ini boot­strap for Doc­trine
$application->getBootstrap()->bootstrap(‘doctrine’);

// Cre­ate the con­fig­u­ra­tion array
$con­fig = $application->getOption(‘doctrine’);
// (Note you can have all of these in application.ini aswell)
$config[’generate_models_options’] = array(
// Define the PHP­Doc Block in the gen­er­ated classes
’php­Doc­Pack­age’ =>‘Test­Bench’,
’php­Doc­Sub­pack­age’ =>‘Mod­els’,
’php­Doc­Name’ =>‘Danny Froberg’,
’php­Do­cE­mail’ =>‘danny@hackix.com’,
’php­DocVer­sion’ =>‘1.0′,
// Define whats what and named how, where.
’suf­fix’ => ‘.php’,
’pearStyle’ => true,
’base­ClassPre­fix’ => ‘Base_’,
// Unless you have cre­ated a cus­tom class or want Default_Model_Base_Abstract
’base­Class­Name’ => ‘Doctrine_Record’,
// Leave this empty as spec­i­fy­ing ‘Base’ will cre­ate Base/Base
’base­Class­es­Di­rec­tory’ => NULL,
// Should make it Zend Frame­work friendly AFAIK
’classPre­fix’ => ‘Default_Model_’,
’classPre­fix­Files’ => false,
’gen­er­ate­Base­Classes’ => true,
’gen­er­ateTable­Classes’ => false,
’pack­ages­Path’ => APPLICATION_PATH . ‘/models’,
’pack­ages­Fold­er­Name’ => ‘packages’,

);

$cli = new Doctrine_Cli($config);
$cli->run($_SERVER[’argv’]);
?>

In the next arti­cle we’ll take it for a test spin and gen­er­ate a few mod­els and such ;)

Enjoy.

5 Responses to "Setting Up Doctrine for Zend Framework 1.9.x"

Leave a Comment

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes