Skip to content

Zend Framework 1.9.6 and Doctrine 1.2.1 table creation from YAML

December 17, 2009

UPDATE
See my latest post on the new best way to get this working.

So, I’ve spent the last while trying, trying, trying (!) to get Zend integrated with Doctrine. I’m almost there! I ran into a couple of problems and found some solutions that I thought I’d share since they were so difficult to come across.

I was having problems getting the Doctrine CLI to work when trying to create models and build the DB from a YAML file. First issue I ran into was the following error when running the task “build-all-reload”:

build-all-reload - Are you sure you wish to drop your databases? (y/n)
y
build-all-reload - Successfully dropped database for connection named 'doctrine'

Fatal error: Class 'sfYaml' not found in /home/jeremy/aptana_workspace/blog/library/Doctrine/Parser/Yml.php on line 80

Doctrine couldn’t find the sfYaml classes (borrowed from Symfony, I’m using Zend). Solution I found (didn’t come up with it myself) was this:

$loader = Zend_Loader_Autoloader::getInstance();
$loader->pushAutoloader(array('Doctrine', 'autoload'), 'Doctrine');
$loader->registerNamespace('sfYaml')->pushAutoloader(array('Doctrine', 'autoload'), 'sfYaml');

That last line is the one that does the trick. Register a new namespace for the sfYaml files (they all begin with “sfYaml”) and then attach that namespace to the Doctrine autoloader.

The next problem I encountered was that the Doctrine CLI task of “build-all-reload” said it was creating the tables in the database, but it wasn’t. No errors, nothing. The database itself was getting dropped and re-created and the model classes were generated without any problem. I finally found a post on the Doctrine Google discussion board that indicated that the following code, when run by the CLI, will stop the tables from getting created.

$manager->setAttribute(
    Doctrine::ATTR_MODEL_LOADING,
    Doctrine::MODEL_LOADING_CONSERVATIVE
);

Comment that line out of your _initDoctrine method or your Application Resource plug-in and you should be able to create tables. Make sure to uncomment when you’re done.

Advertisement
Leave a Comment

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.