I’m starting to use Zend Framework 1.8.4 with Doctrine Object Relational Mapper (because after working on a java/hibernate project, I can’t go back to writing SQL and building databases) With Doctrine ORM, you can define a data model in YAML or XML and it will build the database with foreign keys, etc, etc ,etc… learn more on that here.
On starting my project, I looked around to see what everyone else was doing. Since 1.8.4 is relatively new, and there were some pretty neat changes, there aren’t many good tutorials out there.
That being said, and because of said java/hibernate project I’ve been working on, I’ve become quite familiar with Apache Ant. It’s basically an easy way to do some of the mundane work and sometimes heavy lifting in day to day programming. That, and I use Eclipse IDE, so wasn’t too fond of always running to the command prompt to rebuild my models, create a zend module, etc.
So here’s what I’ve come up with.
I started with this tutorial: Doctrine ORM and Zend Framework It shows you how to create a Zend 1.8.4 project with doctrine. The key aspect is the doctrine-cli.php script.
I should let you know, while I do use Eclipse for most of my projects, a while back I bought Zend Studio for Eclipse. I think it’s much better for writing PHP than any plugin for eclipse (PDT, PHPEclipse). If you write a lot of PHP, ZS4E is very very worth the money. While it does say it is “zend studio for eclipse”, it installs its own installation of eclipse. If you find yourself in this boat, and want to use Ant in Zend Studio for Eclipse, check out this tutorial. It’s rather screwy that you can’t install ant tools easily, but this definitely does the trick.
But I digress… let’s get started.
Here is the directory structure of my project, basically defined by the above tutorial:
- root
- htdocs [zend framework project root]
- application
- [all application folders]
- doctrine
- library
- public
- scripts
- test
- .project
- build.xml
- env.properties
The body of env.properties looks like this:
zf.project.basedir=htdocs
doctrine.scripts.path=${zf.project.basedir}/scripts
And the body of the build.xml looks like this:
<project name="zf-doctrine-project">
<property file="env.properties" />
<target name="doctrine.cli" description="Run a doctrine task on doctrine-cli.php - default reload">
<input message="Enter a task:"
addproperty="doctrine.task"
defaultvalue="build-all-reload" />
<fail unless="doctrine.task" message="No task selected" />
<exec dir="${doctrine.scripts.path}"
command="php doctrine-cli.php ${doctrine.task} y"/>
</target>
</project>
Although it’s painfully self-explanatory, for the sake of completeness, here’s what it does, line by line:
2. Loads in the env.properties file
3. Defines the target, which is like a job/task in ant
4. – 6. Prompts the user to enter a task to pass to the cli script (just like you would pass in the command line)
7. Makes sure the user enters a value
8. – 9. Executes the php script, passing in the entered task as a command line option, and forces yes to “are you sure?”
Now, the fun part.
In your project in Eclipse, open the ant window by going to Window > Show View > Other and select Ant under the Ant folder. When you see the Ant View, either open your ant file in that window using it’s open dialog, or just drag your build.xml to that panel. You’ll see the little white file with the ant on it and whatever you have as the name attribute of the <project> tag in your build xml. Expand that so you can see the targets, and you should see this:

Showing the doctrine task in the eclipse ant view
Now if you double click that target, you should see a prompt for the task you want to run. The default value there will run your schema.yml and rebuild your db and models.
Crafty kids may notice that i have the “Hide internal targets button” turned on in that image… well, those targets are for another show… (damn you crafty kids…)

