Skip to content

Ultimate Zend Framework and Doctrine Integration

November 9, 2010

I’ve been through a lot to get this all working and feel I MUST share! This guide should spell out exactly what you need to get these two great technologies working in harmony. I always forget little quirks each time I start a new project so this will be a reminder to me as well. I’m using Zend Framework 1.11.0 with Doctrine 1.2.3 and MySQL.

Start by creating your Zend application using the zf command line/terminal tool. Next create a folder called doctrine within your application folder at the same level as the configs, controllers, and other folders. Within the doctrine folder create three folders: data, migrations, and schema. Within the data folder create two folders: fixtures and sql. At the same level as your application folder, create a folder named scripts.

There are four key files to this setup – the application’s configuration file, the application’s bootstrap file, the Doctrine cli script, and the yaml model file.

Application Configuration

The following is my baseline application.ini. Yours may vary slightly on a few items like appnamespace and Doctrine connection string.

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = ""
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0

autoloaderNamespaces[] = "Doctrine_"

doctrine.connection_string = "mysql://root@localhost/mydb"
doctrine.cache = false

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1 

Let’s break this down one piece at a time referencing the changes you’ll notice from the default file.

appnamespace = ""

I’ve set my appnamespace to be blank since I don’t want my model classes to be named like Application_Model_User. I just want it to be Model_User. We are going to be using PEAR style model naming where folder levels will dictate an underscore in our class names. So, our model class names will be Model_User, because they live inside the Zend Framework models folder. Our base class names are structured Model_Base_User since they reside in models/Base.

autoloaderNamespaces[] = "Doctrine_"

I’ve set up an additional namespace for Zend Framework to autoload: Doctrine_. We’ll see later that we will tell Doctrine we are going to be using PEAR naming so when it encounters a class it needs and it begins with the Doctrine_ prefix, it will know where to find it based on our PEAR naming scheme.

doctrine.connection_string = "mysql://root@localhost/mydb"
doctrine.cache = false

We set our connection string which is how Doctrine will know to connect to our database. I’ve got query caching turned off for now.

Application Bootstrap

Next comes our application’s Bootstrap.php file. Add a Doctrine function for initialization:

protected function _initDoctrine()
{
	$config = $this->getOption('doctrine');

	$manager = Doctrine_Manager::getInstance();
	$manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, 'Model_');
	$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_PEAR);
	$manager->setAttribute(Doctrine_Core::ATTR_VALIDATE, Doctrine_Core::VALIDATE_ALL);
	$manager->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, true);
	$manager->setAttribute(Doctrine_Core::ATTR_AUTO_FREE_QUERY_OBJECTS, true);
	$manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
	$manager->setAttribute(Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true);

	if (isset($config['cache']) && $config['cache'] == true) {
            $cacheDriver = new Doctrine_Cache_Apc();
            $manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver);
        }

	$connection = $manager->openConnection($config['connection_string'], 'doctrine');
	$connection->setAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM, true);
	$connection->setCharset('utf8');

	return $connection;
}

Let’s break it down again.

$config = $this->getOption('doctrine');

Next, we grab all the doctrine settings from our application.ini file and store them in the $config array.

$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, 'Model_');
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_PEAR);
$manager->setAttribute(Doctrine_Core::ATTR_VALIDATE, Doctrine_Core::VALIDATE_ALL);
$manager->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTO_FREE_QUERY_OBJECTS, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true);

Now, we create our Doctrine Manager instance and pass it some configuration settings. We tell Doctrine that our model class names will be prefixed with Model_. We are using PEAR naming, We want Doctrine to validate our queries against our schema. We want to allow DQL callbacks. Doctrine will automatically free query objects when it detects we aren’t using them anymore. We can override default getter/setter functions in our models. We want to use table classes and Doctrine will load them for us.

if (isset($config['cache']) && $config['cache'] == true) {
    $cacheDriver = new Doctrine_Cache_Apc();
    $manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver);
}

Check if we want to use the APC cache driver (recommended) for query caching (not result caching). I’ve got that turned off for now.

$connection = $manager->openConnection($config['connection_string'], 'doctrine');
$connection->setAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM, true);
$connection->setCharset('utf8');

Create a connection to the database using our connection string from the config array, then set a couple of options on the connection to allow enums and use UTF-8 encoding.

return $connection;

Returning the connection makes it available to other application resources (_init functions), although I’ve never had to use it.

Doctrine cli

Within the scripts folder you created at the beginning, create a blank php file named doctrine.php. This will be our Doctrine cli script that will help us to automatically generate our Doctrine model files. Add the following code to the file:

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// 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->getAutoloader()->pushAutoloader(array('Doctrine_Core', 'autoload'));

$application->getBootstrap()->bootstrap('doctrine');

$config = $application->getOption('doctrine');

$options = array(
	'data_fixtures_path' => APPLICATION_PATH . "/doctrine/data/fixtures",
	'sql_path' => APPLICATION_PATH . "/doctrine/data/sql",
	'migrations_path' => APPLICATION_PATH . "/doctrine/migrations",
	'yaml_schema_path' => APPLICATION_PATH . "/doctrine/schema",
	'models_path' => APPLICATION_PATH . "/models",
	'generate_models_options' => array(
		'pearStyle' => true,
		'generateTableClasses' => true,
		'generateBaseClasses' => true,
		'baseClassPrefix' => 'Base_',
		'baseClassesDirectory' => null,
		'classPrefixFiles' => false,
		'classPrefix' => 'Model_'
	)
);

$config = array_merge($config, $options);

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

This should look almost exactly like your application’s index.php file located in the public folder with a few variations.

$application->getAutoloader()->pushAutoloader(array('Doctrine_Core', 'autoload'));

We add the Doctrine autoload class to the Zend Framework autoloader. As far as I know, the only purpose this serves is to be able to autoload the Symfony yaml parsing libraries which are included with Doctrine (which don’t follow PEAR style naming). We need these libraries to parse our schema yaml file (and fixtures).

$application->getBootstrap()->bootstrap('doctrine');

Instead of bootstrapping and running the application, we are going to get the application’s bootstrap and run the _initDoctrine() function we created. This will handle our database connection for us.

$config = $application->getOption('doctrine');

Next, we get the Doctrine options from the application.ini.

$options = array(
	'data_fixtures_path' => APPLICATION_PATH . "/doctrine/data/fixtures",
	'sql_path' => APPLICATION_PATH . "/doctrine/data/sql",
	'migrations_path' => APPLICATION_PATH . "/doctrine/migrations",
	'yaml_schema_path' => APPLICATION_PATH . "/doctrine/schema",
	'models_path' => APPLICATION_PATH . "/models",
	'generate_models_options' => array(
		'pearStyle' => true,
		'generateTableClasses' => true,
		'generateBaseClasses' => true,
		'baseClassPrefix' => 'Base_',
		'baseClassesDirectory' => null,
		'classPrefixFiles' => false,
		'classPrefix' => 'Model_'
	)
);

Now we set the paths to all those Doctrine folders we created at the beginning. These settings will tell the Doctrine cli where to find and store our files. Finally, we set some additional options which tell the Doctrine cli how we want our models structured – using PEAR style, with table classes and base classes, with a base class prefix of Base_, with no additional base class directory besides Base, with no prefix on our class file names besides Model_, having a class name prefix of Model_ for our main model and table classes.

$config = array_merge($config, $options);

Now we merge the Doctrine options from our appllication.ini with those that we have set here.

$cli = new Doctrine_Cli($config);

Pass the Doctrine options to a new instance of the Doctrine cli class.

$cli->run($_SERVER['argv']);

Finally, we run the cli.

Yaml Schema

The last file we need to create is our yaml schema file. In the doctrine/schema folder you created at the beginning, create a file named schema.yml. We’ve already told the cli to look in this folder earlier for our schema files. This is the file that Doctrine will use to generate our models and our database for us. We’ll just set up a very basic User model at this point. There are a lot of configuration options you can use with Doctrine and your schema files not covered here. Put the following in the schema.yml file you just created:

User:
  columns:
    username: string(20)
    password: string(20)

Remember that yaml files are very sensitive about indentation. Those are two space indentations between the three sections.

Model Creation

We’ve finally come to the fun part – creating our models! Open up a command prompt or terminal and navigate to the scripts folder of your application. You can also create a shell script or batch file to run the cli if you don’t like calling it directly from php like I’m doing here. Run the following command to make sure that you can access the Doctrine cli correctly:

php doctrine.php

If all is well, you should see a bunch of options appear. We’re now ready to create our actual models. Run the following command:

php doctrine.php generate-models-yaml

This tells the Doctrine cli to run the task that will generate our models for us based on the yaml file we passed it. You should now see some new files in your application’s models directory.

If starting from scratch, you’ll also want to have the cli create your database schema for you. The easiest way to do this is to run the following command. You’ll probably have to at least create your database first:

php doctrine.php build-all-reload

This will drop the database (hence why you have to create it first), re-create it, build the models, and create your tables in your database. Pretty sweet!

You should now be able to use your models in your application. Try it out by editing your index controller’s index action.

$user = new Model_User();
$user['me'];
$user['password'] = 'secret';
$user->save();

$this->view->user = $user->toArray();

That will create a new user and save it to your database. Check your database and the new user should be there.

Then in your view file echo out your user:

echo 'My name is ' . $this->user['username'] . ' and my password is ' . $this->user['password'];

Fire up your browser and take a look.

Hopefully this will get you going. Let me know if you see any mistakes or have any optimizations.

Advertisement
14 Comments
  1. Hi.

    Nice step by step tut.

    I followed up all you wrote and I got stuck at point where it says
    run in terminal:
    php doctrine.php

    i ran my cmd and went to scripts folder and after i ran that command i got error:

    PHP Fatal error: Uncaught exception ‘Zend_Config_Exception’ with message ‘parse
    _ini_file(\\CONFIGS\APPLICATION.INI): failed to open stream: No such file or dir
    ectory’ in C:\wamp\www\estudent\library\Zend\Config\Ini.php:181
    Stack trace:
    #0 C:\wamp\www\estudent\library\Zend\Config\Ini.php(201): Zend_Config_Ini->_pars
    eIniFile(‘/configs/applic…’)
    #1 C:\wamp\www\estudent\library\Zend\Config\Ini.php(125): Zend_Config_Ini->_load
    IniFile(‘/configs/applic…’)
    #2 C:\wamp\www\estudent\library\Zend\Application.php(383): Zend_Config_Ini->__co
    nstruct(‘/configs/applic…’, ‘production’)
    #3 C:\wamp\www\estudent\library\Zend\Application.php(85): Zend_Application->_loa
    dConfig(‘/configs/applic…’)
    #4 C:\wamp\www\estudent\application\scripts\doctrine.php(34): Zend_Application->
    __construct(‘production’, ‘/configs/applic…’)
    #5 {main}
    thrown in C:\wamp\www\estudent\library\Zend\Config\Ini.php on line 181

    Fatal error: Uncaught exception ‘Zend_Config_Exception’ with message ‘parse_ini_
    file(\\CONFIGS\APPLICATION.INI): failed to open stream: No such file or director
    y’ in C:\wamp\www\estudent\library\Zend\Config\Ini.php:181
    Stack trace:
    #0 C:\wamp\www\estudent\library\Zend\Config\Ini.php(201): Zend_Config_Ini->_pars
    eIniFile(‘/configs/applic…’)
    #1 C:\wamp\www\estudent\library\Zend\Config\Ini.php(125): Zend_Config_Ini->_load
    IniFile(‘/configs/applic…’)
    #2 C:\wamp\www\estudent\library\Zend\Application.php(383): Zend_Config_Ini->__co
    nstruct(‘/configs/applic…’, ‘production’)
    #3 C:\wamp\www\estudent\library\Zend\Application.php(85): Zend_Application->_loa
    dConfig(‘/configs/applic…’)
    #4 C:\wamp\www\estudent\application\scripts\doctrine.php(34): Zend_Application->
    __construct(‘production’, ‘/configs/applic…’)
    #5 {main}
    thrown in C:\wamp\www\estudent\library\Zend\Config\Ini.php on line 181

    And with Bootstrap file?! Do you only have that function in the Bootstrap or is there some init or construct you have beside that. I have it like this:

    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
    {

    protected function _initDoctrine()
    {
    $config = $this->getOption(‘doctrine’);

    $manager = Doctrine_Manager::getInstance();
    $manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, ‘Model_’);
    $manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_PEAR);
    $manager->setAttribute(Doctrine_Core::ATTR_VALIDATE, Doctrine_Core::VALIDATE_ALL);
    $manager->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, true);
    $manager->setAttribute(Doctrine_Core::ATTR_AUTO_FREE_QUERY_OBJECTS, true);
    $manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
    $manager->setAttribute(Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true);

    if (isset($config['cache']) && $config['cache'] == true) {
    $cacheDriver = new Doctrine_Cache_Apc();
    $manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver);
    }

    $connection = $manager->openConnection($config['connection_string'], ‘doctrine’);
    $connection->setAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM, true);
    $connection->setCharset(‘utf8′);

    return $connection;
    }

    }

    And last of all i am using NetBeans, maybe its problem in that?

    Thanks for your help in advance.

  2. Ok my bad i saw that application folder and scripts foledr needs to be on same level. Now i get this error:

    PHP Warning: include_once(Doctrine\Manager.php): failed to open stream: No such
    file or directory in C:\wamp\www\estudent\library\Zend\Loader.php on line 146
    PHP Warning: include_once(): Failed opening ‘Doctrine\Manager.php’ for inclusio
    n (include_path=’C:\wamp\www\estudent\application/../library;C:\wamp\www\estuden
    t\library;.;C:\php5\pear;c:\wamp\www\estudent\library’) in C:\wamp\www\estudent\
    library\Zend\Loader.php on line 146
    PHP Warning: include_once(Doctrine\Core.php): failed to open stream: No such fi
    le or directory in C:\wamp\www\estudent\library\Zend\Loader.php on line 146
    PHP Warning: include_once(): Failed opening ‘Doctrine\Core.php’ for inclusion (
    include_path=’C:\wamp\www\estudent\application/../library;C:\wamp\www\estudent\l
    ibrary;.;C:\php5\pear;c:\wamp\www\estudent\library’) in C:\wamp\www\estudent\lib
    rary\Zend\Loader.php on line 146
    PHP Warning: call_user_func() expects parameter 1 to be a valid callback, class
    ‘Doctrine_Core’ not found in C:\wamp\www\estudent\library\Zend\Loader\Autoloade
    r.php on line 124
    PHP Warning: call_user_func() expects parameter 1 to be a valid callback, class
    ‘Doctrine_Core’ not found in C:\wamp\www\estudent\library\Zend\Loader\Autoloade
    r.php on line 124
    PHP Fatal error: Class ‘Doctrine_Manager’ not found in C:\wamp\www\estudent\app
    lication\Bootstrap.php on line 10

    I guess it doesn’t find my Doctrine library…

  3. Ok. Now i got all up and running i added my Doctrine path in php.ini file. Now only prob is how is your index.php set up in public folder…

    • That’s really quite odd that it couldn’t find your Doctine folder. I always just put my Doctrine folder in the library folder of the Zend Framework application. I have a feeling you’re going to continue to have problems after you get it set up all the way if it was already having problems finding your Doctrine folder

      I just have the default index.php file that Zend Tool creates when you create a new Zend Framework project.

  4. Could be that my Bootstrap file isn’t set correctly…

    Well i have Zend and Doctrine in library in my project

    library -> Zend -> etc…
    library -> Doctrine -> Doctrine(folder) + Doctrine.php

    all other application.ini and rest of the structure is as you explained….

    • That’s how I have it set up too. I also use Netbeans so I doubt that is an issue. I looked at your bootstrap from above and that’s also how I have it set up. I’m using Zend Server here locally on my Windows box, but I also run this on a Linux box with this setup without problems.

  5. sone permalink

    Thanks for your tutorial. I have tried many and since this is my first time I have yet to get any to run correctly. Your tutorial seems more recent and closely matches the current release of doctrine 1.2 and zend framework 1.9.

    I have followed your steps but have tried it with an existing project that is now structured as modules.

    The part I am confused about is “Model Creation”

    “Open up a command prompt or terminal and navigate to the scripts folder of your application”

    Do you mean the scripts folder we created earlier where the doctrine-cli sits? There is n doctrine.php in this folder. what scrips folder are you referring to?

    running php doctrine.php does not work.

    I have tried running the cli like from pervious tutorials but get a different error

    si2:si3 sone$ ./scripts/doctrine-cli generate-models-yaml
    ./scripts/doctrine-cli: line 1: //: is a directory
    ./scripts/doctrine-cli: line 2: syntax error near unexpected token `’APPLICATION_PATH”
    ./scripts/doctrine-cli: line 2: `defined(‘APPLICATION_PATH’)’

    Any ideas what I can try next?

    Thanks

    • If you using a modular structure, then depending on how you have things organized (separate models folder for each module for instance) then model creation probably won’t work as expected. This tutorial isn’t geared toward module based projects so it’s not going to work as you expect.

      Yes, for the scripts folder, I was referring to the one created as part of the tutorial. A step of the tutorial is to create the doctrine.php file – it obviously won’t be there automatically. Sounds like you are trying to mix tutorials too much. Since I can’t see your doctrine-cli file I have no way of knowing why you’re getting the error message that you are.

  6. This wont work with the latest Doctrine.

    • And I say as much at the beginning of the article by indicating that I used Doctrine 1.2.3. Doctrine 2 is pretty much a complete rewrite and is not backwards compatible at all.

  7. Ambi Sidhu permalink

    Thanks for the great info.

    I am nearly done with my first Doctrine 1.2.3 + ZF 1.11.4 and its been a great experience … recommended!

    At the end of my development and beginning of the sprint to soft launch, I am eager to get a good migrations scheme in place.

    My question has to do with getting the doctrine cli function generate-migrations-diff to work. It always seems to die with: “Fatal error: Cannot redeclare class Model_Base…”

    I checked out what the script tries to do and it seems that it copies in the models directory (mine are prefixed with “Model_” and the sub-dir Base (where models are prefixed with Model_Base_) to a tmp dir called fromprfx_doctrine_tmp_dirs and tries to compare these models to ones generated from the altered schema in a dir called toprfx_doctrine_tmp_dirs. The Models in toprfx_doctrine_tmp_dirs are named like this: class ToPrfxCatalog.

    I have tried to modify the parameters to no avail:
    doctrine.model_autoloading = 1
    doctrine.model_loading = 1

    Here are my some of my configs:

    ** Bootstrap:
    protected function _initDoctrine() {
    //the following line seems to solve a Base model load issue
    $moduleLoad = new Zend_Application_Module_Autoloader(array(
    ‘namespace’ => ”,
    ‘basePath’ => APPLICATION_PATH
    ));
    $this->getApplication()->getAutoloader()
    ->pushAutoloader(array(‘Doctrine’, ‘autoload’));
    spl_autoload_register(array(‘Doctrine’, ‘modelsAutoload’));

    $doctrineConfig = $this->getOption(‘doctrine’);
    $manager = Doctrine_Manager::getInstance();
    $manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
    $manager->setAttribute(
    Doctrine_Core::ATTR_MODEL_LOADING,
    $doctrineConfig['model_autoloading']
    );
    $manager->setAttribute(Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true);

    Doctrine_Core::setModelsDirectory($doctrineConfig['models_path']);
    Doctrine_Core::loadModels($doctrineConfig['models_path']);

    $conn = Doctrine_Manager::connection($doctrineConfig['dsn'], ‘doctrine’);
    $conn->setAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM, true);
    return $conn;
    }

    ** application.ini
    autoloaderNamespaces[] = “Doctrine”
    autoloaderNamespaces[] = “GD”

    doctrine.dsn = “mysql://***:***@192.168.1.12/***_live”
    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”

    doctrine.generate_models_options.pearStyle = true
    doctrine.generate_models_options.generateTableClasses = false
    doctrine.generate_models_options.generateBaseClasses = true
    doctrine.generate_models_options.baseClassPrefix = “Base_”
    doctrine.generate_models_options.baseClassesDirectory =
    doctrine.generate_models_options.classPrefixFiles = false
    doctrine.generate_models_options.classPrefix = “Model_”

    doctrine.model_autoloading = 2

    [doctrineCLIdevelopment : development]
    ; Aggressive Model Loading
    doctrine.model_autoloading = 1
    phpSettings.display_startup_errors = 1
    phpSettings.display_errors = 1

    [doctrineCLIproduction : production]
    ; Aggressive Model Loading
    doctrine.model_autoloading = 1
    phpSettings.display_startup_errors = 0
    phpSettings.display_errors = 0

    ** Doctrine.php in applications/scripts

    // Define path to application directory
    defined(‘APPLICATION_PATH’)
    || define(‘APPLICATION_PATH’, realpath(dirname(__FILE__) . ‘/..’));

    // Define application environment
    if(!defined(‘APPLICATION_ENV’)) {
    $user = $_SERVER["USER"];
    //this will only work in my development enviro … note we cannot access server_name from CLI
    if($user == “ambisidhu”) {
    define(‘APPLICATION_ENV’, ‘doctrineCLIdevelopment’);
    }else {
    define(‘APPLICATION_ENV’, ‘doctrineCLIproduction’);
    }
    }

    // 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’);

    $config = $application->getOption(‘doctrine’);

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

    • Compare your doctrine init method to what I have above. One main difference I spotted is that I am using MODEL_LOADING_PEAR as opposed to MODEL_LOADING_AGGRESSIVE or MODEL_LOADING_CONSERVATIVE. Looks like there might be other differences between our configurations as well.

      • Ambi Sidhu permalink

        Yes, I tried passing in 3 for that constant… in fact I used your exact set up and still had issues.

        I am going to proceed with making migration classes manually for now.

        One more question though: Did you ever get generate-migrations-diff to work using a changed schema.yml and models named like mine with the Base classes subdir? I have tried to debug how the diff script works and I can’t even get it to compare the Base models to the models it generates from the schema. The Model_User extends Model_Base_User for example has only my custom methods in it, while the Base/User.php (abstract class Model_Base_User extends Doctrine_Record) has all the schema related info.

Trackbacks & Pingbacks

  1. Zend Framework 1.9.6 and Doctrine 1.2.1 table creation from YAML « Things I've Learned

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.