Setting Up Doctrine for Zend Framework 1.9.x

Setting Up Doctrine for Zend Framework 1.9.x

After some fiddling and googling and Zendcast watching 🙂 I figured out how to get the models to generate the way I needed them to.
And figuring out why generate-sql and build-all-reload did not create sql schema nor create any tables in the mysql database.

Directory Structure

Standard ZF directory structure with a few addition (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 applicable parts added.

[production]
phpSettings.date.timezone = "Europe/Stockholm"
autoloaderNamespaces[] = "Doctrine"
doctrine.dsn                = "mysql:[email protected]/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 comment about model loading attributes, this is what broke it for me, for some reason using conservative will prevent generation and creation of database tables and schemas, although model generation works fine, very puzzling!.

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    protected function _initDoctrine() {
        $this->getApplication()
             ->getAutoloader()
             ->pushAutoloader ( array ('Doctrine', 'autoload' ) );
        //spl_autoload_register ( array ('Doctrine', 'modelsAutoload' ) );
        $manager = Doctrine_Manager::getInstance ();
        $manager->setAttribute ( Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true );
        // The Model Loading 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 );

        $doctrineConfig = $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 Windows 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

doctrine

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

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

doctrine.php

Please note the inline comments in this one and customize it to your own liking.

<?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';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

// Read in the application.ini bootstrap for Doctrine
$application->getBootstrap()->bootstrap('doctrine');

// Create the configuration array
$config = $application->getOption('doctrine');
// (Note you can have all of these in application.ini aswell)
$config['generate_models_options'] = array(
    // Define the PHPDoc Block in the generated classes
    'phpDocPackage'         =>'TestBench',
    'phpDocSubpackage'      =>'Models',
    'phpDocName'            =>'Danny Froberg',
    'phpDocEmail'           =>[email protected]',
    'phpDocVersion'         =>'1.0',
    // Define whats what and named how, where.
    'suffix'                => '.php',
    'pearStyle'             => true,
    'baseClassPrefix'       => 'Base_',
    // Unless you have created a custom class or want Default_Model_Base_Abstract
    'baseClassName'         => 'Doctrine_Record',  
    // Leave this empty as specifying 'Base' will create Base/Base
    'baseClassesDirectory'  => NULL,
    // Should make it Zend Framework friendly AFAIK              
    'classPrefix'           => 'Default_Model_',   
    'classPrefixFiles'      => false,
    'generateBaseClasses'   => true,
    'generateTableClasses'  => false,
    'packagesPath'          => APPLICATION_PATH . '/models',
    'packagesFolderName'    => 'packages',
 
);

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

In the next article we'll take it for a test spin and generate a few models and such 😉

Enjoy.

5 thoughts on “Setting Up Doctrine for Zend Framework 1.9.x

  1. Great tutorial. many thanks to you for that.
    You can upgrade your doctrine.bat file like that:
    @echo off
    echo Running Doctrine CLI.
    “php.exe” -f %~dp0\doctrine.php %1 %2 %3 %4 %5 %6 %7 %8 %9

    of course if you have php.exe in your PATH dir. However variable %~dp0 gives directory of the executing file (dir of the doctrine.bat in this case).

  2. nice tutorial, very good. but i have another question. How can i use all Doctrines’ commands from netbeans command windows?

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.