Deprecated: This code uses an old version of Doctrine and Zend Framework, there is now an semi-official way of integrating Doctrine and Zend Framework, see the announcement of the zf-doctrine project and checkout my plugin for ZFDebug to have access to the Doctrine Profiler: http://github.com/danceric/zfdebugdoctrine
—
This is my notes on integrating the Doctrine ORM with a Zend Framework project that use Zend_Application. This post was largely inspired by the post Integrating Zend Framework and Doctrine. In fact, you can see mine as an updated version of this great post to work in ZF 1.8. Ruben Vermeersch, the author, have stated on his blog that he want to update the post but he don’t have the time yet, so this is me trying to help.
This was done on OS X, so the instructions here assume a n*x like environment. You should also be comfortable with Zend Framework 1.8+ and the new Zend_Application. As stated in the ZF quickstart tutorial, you can create a project by running this command from the uncompressed Zend Framework folder:
./bin/zf.sh create project ~/Sites/zfdoctrine
In the newly created directory, which is a complete zf application, create the directories that will be needed by doctrine
cd ~/Sites/zfdoctrine
mkdir doctrine doctrine/migrations doctrine/schema
mkdir doctrine/data doctrine/data/fixtures doctrine/data/sql
Now, put the content of the Doctrine (1.0 or 1.1) lib folder in library. I strongly suggest that you put the ZF library here too. If you manage more that one project, it’s very unlikely that you will have time to update all your projects at the same time, which is needed if you use a global include path.
Create the Doctrine configuration in application/configs/application.ini
;doctrine.connection_string = "mysql://root:pwd@localhost/zfdoctrine" doctrine.connection_string = "sqlite:///" APPLICATION_PATH "/zfdoctrine.db" doctrine.data_fixtures_path = APPLICATION_PATH "/../doctrine/data/fixtures" doctrine.models_path = APPLICATION_PATH "/models" doctrine.migrations_path = APPLICATION_PATH "/../doctrine/migrations" doctrine.sql_path = APPLICATION_PATH "/../doctrine/data/sql" doctrine.yaml_schema_path = APPLICATION_PATH "/../doctrine/schema"
Initialize Doctrine in you application by adding this function to application/Bootstrap.php
public function _initDoctrine() { require_once 'Doctrine.php'; $loader = Zend_Loader_Autoloader::getInstance(); $loader->pushAutoloader(array('Doctrine', 'autoload')); $doctrineConfig = $this->getOption('doctrine'); $manager = Doctrine_Manager::getInstance(); $manager->setAttribute( Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_CONSERVATIVE); // Add models and generated base classes to Doctrine autoloader Doctrine::loadModels($doctrineConfig['models_path']); $manager->openConnection($doctrineConfig['connection_string']); return $manager; }
One of the fun thing about Doctrine is that you have access to a lot of command line tool to create your database, models, sql, schema, etc. Create a new file called doctrine-cli in a scripts folder at the root of you project with the following content:
#!/usr/bin/env php <?php /** * Doctrine CLI script */ define('APPLICATION_ENV', 'development'); define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . '/../library'), get_include_path(), ))); 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'); $cli = new Doctrine_Cli($application->getOption('doctrine')); $cli->run($_SERVER['argv']);
You can make this script executable and try it, to see the list of task that doctrine-cli can do for you
chmod +x ./scripts/doctrine-cli
./scripts/doctrine-cli
Now you can jump back to [Ruben Vermeersch's Tutorial]http://ruben.savanne.be/articles/integrating-zend-framework-and-doctrine) tutorial in the “Building an application” section and create the sample application. You’ll learn how to use Doctrine do a simple insert, a query and how to use the command line to create your database and model.
note: If you’re using these two projects together, you may be interested in my next post too: ZFDebug and Doctrine ORM
update 2009/10/29: things are getting easier, check out: Doctrine 1.2 is Zend Framework friendly
Hi, Eric,
Nice entry. I have one question. In doctrine-cli, you write
In this line , you want to execute just “_initDoctrine()” method in Bootstrap? If it’s so, I think you need to modify it as
$application->getBootstrap()->bootstrap(‘doctrine’);
Thanks.
Pingback: Zend Framework Quick StartにDoctrineを適用してみる - その2 - KDF Memo
Thanks for this helpful article.
I have encountered a problem with the doctrine-cli script in line 12: It overwrites the include paths defined in the php.ini. Maybe you should use the same set_include_path() which is used in public/index.php, because it remains the settings by adding get_include_path():
set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . ‘/../library’), get_include_path(), )));
@Yuta, Thanks for the fix, I this exactly what I was trying to do, load only the _initDoctrine() method
@Fabian, You’re right, I have updated the code. I have the strange habit of overriding the include_path when I deploy on shared hosting to have full control on it
Thank you very much for the great example. Could you tell me how this one could be used modules? So that it works with multiple models folders. Is there a way to configure Doctrine to work in such an environment?
Thank you very much.
Pingback: Ants in my Zend Framework (with doctrine for good measure) | sproif.com
Very useful! Thank you! I got it running in a matter of minutes. Thinking I saw doctrine for the first time this morning
Thank you for this article.
awesome article, thanks! i’ve never used doctrine until this afternoon, but i love love that you set up autoloading in the nice zf1.8 bootstrap, and wrote the doctrine-cli wrapper to go through the application. really really well done.
if anyone out there who also want only use the zend autoloader this link maybe interesting, zend core dev matthew (mwo) explains how
http://www.nabble.com/Autoloader-issue-td24426857.html
works great!
Matthew Weier O’Phinney from the Zend Framework fame have posted his thought on the subject, take a look at it, it’s probably better than my way of doing things
http://weierophinney.net/matthew/archives/220-Autoloading-Doctrine-and-Doctrine-entities-from-Zend-Framework.html
Pingback: Integrando o Doctrine com o Zend Framework
Pingback: Doctrine ORM and Zend Framework « Danceric
Eric great post . This will be helpful for those who are looking to configure doctrine ORM with ZF . Though I have not used ORM I would love to try it out
.
Thanks for your post to help all ZF developers .
Hi, Thanks for the info, I had this working well and it allowed me to get Doctrine up and running quickly and as I’m new to Doctrine that was great. I was using Doctrine 1.1, but can you tell me if 1.2 can be set up the same way? I tried just swapping the Doctrine 1.2 files in but things don’t seem to work unfortunately.
Thanks again,
Chris
Hi Chris, I haven’t tried Doctrine 1.2 with this setup yet, but as soon as the beta for 1.2 land, I’ll have it working and I will update my post accordingly
Thank you very much! This post really helped. I having WAMP, ZF 1.9.4 and Doctrine 1.1.4. No problems with setup even on Windows.
I’m playing around with the latest alpha of Doctrine 1.2 and the future seems really bright. It seems way easier to use it with ZF now. Can’t wait for the beta
Pingback: Zend Framework and Doctrine | Hot Dorkage