Deep Integration between Zend and Doctrine 1.2
January 13th, 2010There’s been a lot of talk online about finding the best approach for bringing Zend and Doctrine 1.x together. This video is my humble approach of combining some of the learning brought about over the last few weeks on Zendcasts, as well as suggestions from Doctrine developers.
The goal of this video is to show how you leverage the existing resource loading tools in Zend to have a model structure that reflects Zend’s best practices. This video builds on the last Doctrine video, but if you’re familiar with both frameworks, you should be able to follow along. Enjoy!
Grab a copy of the project or browse the repository.
Also, a big thank you to ServerGrove for extending their support of Zendcasts for January. ServerGrove specializes in Zend Framework hosting and they’ve offered a 10% rebate on hosting with coupon code “zc”. If you’re looking for a host, be sure to check them out. They’ve also added an additional coupon for “Mini Hosting” plans, get $2 off by using code “zcmini”.

Good job.
This is some tips :
you don’t need to set ‘aggressive’ for the cli and ‘conservative’ for the ‘http’ request.
You don’t need to use Doctrine.php
You don’t need to copy/link vendor.
how ?
In _initDoctrine :
remove
Doctrine,autoload and modelsAutoload
add (need only for the class ’sfYmal’ )
$this->getApplication()->getAutoloader()->pushAutoloader(array(‘Doctrine_Core’, ‘autoload’));
add
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, ‘Model_’);
update
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_PEAR);
update all ref Doctrine to Doctrine_Core
That’all.
Why did you create the _initAppAutoload method?
this ensures that we get the proper Model_ loading behaviour since models are considered a “resource” in Zend (like the application/forms/ folder contains Form_ objects.) Not sure if this is a hold-over or if ZF is moving more in this direction though…
Isn’t the right way for setting up doctrine implementing application resource, instead having _initDoctrine in your bootstrap?
[...] on the ZendCasts.com site is this recent screencast about the best approach for integration of the popular Doctrine ORM and the Zend Framework. his [...]
@umpirsky: The application resource is a good move, however it adds yet another level of indirection, which when you’re trying to get the two frameworks to work properly initially, can simply add confusion. I’ve seen implementations of the application resource, however if you’re not going to be running the resource in more than one place and its a database-driven website, what’s the point?
I watched all the casts from this awesome site, and just wanted to thank you for what you’re doing!!!
In the past I have used
Doctrine_Core::loadModels($doctrineConfig['models_path']);
in my _initDoctine method instead of creating the _initAppAutoload method. I guess that’s where my confusion came from. Any real difference behind the scenes between the two approaches?
Great screencast! Keep them coming.
Hm… everything worked fine (except of the things you already mentioned) until I changed from CONSERVATIVE to AGGRESSIVE when using CLI. Now when I have deleted all Model and Base/Model classes and run build-all-reload it works, DB is created, tables are creates, fixtures are filled and the Application runs without erros afterwards. BUT if I do changes to the schema and want to run build-all-reload again (now Models and auto-generated Base Model files exist) I get a fatal error:
Fatal error: Class ‘Model_Base_Album’ not found in C:\…\Album.php on line 14
So the generated Base Models are not found. Can anyone help me with this?
Unbelievable… I renamed “Album” to “Music” and it worked? WTF???
@ahue
I think the reason could be that aggressive loading just recurses through your models directory and loads everything it finds. Since “Album” is alphabetically before “Base”, the Base_ classes just aren’t loaded yet when it gets to Album.php. When it loads Music.php, it has already recursed into the Base directory and loaded the base classes.
WTF???
Hehe, the “a” Problem. Every Model starting with an a causes that problem. Give it a try, strange voodoo!
Hi, after I’ve set the model loading to aggressive creating the tables failed silently. I figured out that there is an issue with class_exists php internal function at line 782 in doctrine_core class’s isValidModelClass function. Somehow it throws a silent error that preventing to function properly I replaced it with “in_array($class, get_declared_classes())” and the tables have been created successfully. That’s probably my compiled MAMP 1.8.2 or ZF’s (1.9.5) autoload mechanism related bug or something other.
If somebody encountered the same problem maybe that helps.
r. Sandor
same problem.. I need at least three models beginning at ‘a’, so it’s quite fed-upping every time delete this three models and recovering all methods after rebuild models or db..
any solution Jon?
)
that’s a pretty strange bug! I’d try and hold out for Doctrine 2 if I could… The Doctrine team is pretty responsive as well, I would shoot them an email or check the bug tracker.
Hi Jon, thanks for your casts, they are very interesting. But after the “deep integration” of doctrine my phpunit test fails.
“create-tables – Created tables successfully
SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘apptest.car’ doesn’t exist”
My database “apptest” ist created but there is no table in it.
greeting
tobias
[...] figure out what was going on, not to find a solution, I did that downloading the application from here and looking into their code.) This will get rid of the generated folder and instead create all base [...]
Tobias, try putting
doctrine.model_autoloading = 1
in your “testing” section of application.ini if it’s not there already.
[...] Jon Lebensold hat ein Video über die Integration von Doctrine 1.2 veröffentlicht. [...]