2009-10-29 20:37:35
I've talked about that before on this blog. Using Doctrine 1.1 with ZendFramework 1.8 and using both with ZFDebug. Now things will only get better as both project will officially play nice together. Here's the announcement on the Zend Framework mailing list.
This is some pretty good news, the other good news is that you don't have to wait until the integration is ready to start playing with both. I've been testing out the alpha3 release of Doctrine 1.2 and I'm pleased to see that it's really PEAR friendly, which make it a breeze to use it with the Zend Framework (that wasn't as easy with Doctrine 1.1).
you can follow along with the code on github of my demo app: github.com/danceric/zfdebugdoctrine
First thing, the base doctrine class have been moved from /Doctrine.php to /Doctrine/Core.php, which mean that the Zend autoloader can now find it without special config/hack, just add Doctrine_ as a namespace and let ZF do the magic. This can be done in your application.ini with this single line
autoloaderNamespaces[] = "Doctrine_"
Second thing, which is the nice part, is that there is now a PEAR Style Model Loading and Generation, which means that the Doctrine cli can now generate models that can be autoloaded by ZF too. To generate your model from a yaml file (in ./doctrine/schema/schema.yaml`, you can run the doctrine-cli script like this
php ./scripts/doctrine-cli generate-models-yaml
and they will be generated in ./application/models. Note the generate_models_options array of options that define how the model classes will be named. Now, If you wish, you can move the models around to put them in a module by prefixing the classname with modulename_ as you would normally do. If you love spending more time in php land, you can build your model directly in php instead of the yaml file, just by extending the Doctrine_Record class. Check out the Doctrine Documentation, it's really easy to do.
One thing to keep in mind, just as your web pages read the environment setting (APPLICATION_ENV) from ./public/.htaccess or ./public/index.php, the command line script for doctrine have to be set to use the expected environment too (3rd line of the script).
Once again, here's the link to the working application that use Doctrine 1.2alpha3, Zend Framework 1.9.5 and ZFDebug 1.5
github.com/danceric/zfdebugdoctrine
add the doctrine namespace to your autoloader in your application.ini
autoloaderNamespaces[] = "Doctrine_"
connect to a database in your boostrap as
$manager = Doctrine_Manager::getInstance(); $manager->openConnection('your-connection-dsn');
Create your models in the application/models folder by having class that extends Doctrine_Record
class Model_Base_Post extends Doctrine_Record { public function setTableDefinition() { $this->setTableName('post'); $this->hasColumn('id', 'integer', 4, array( 'primary' => true, 'autoincrement' => true, 'type' => 'integer', 'length' => '4', )); $this->hasColumn('content', 'string'); } public function setUp() { parent::setUp(); $timestampable0 = new Doctrine_Template_Timestampable(); $this->actAs($timestampable0); } }