So I’ve been using The framework and Zend_Db for a long tine now and I’m happy using it for some of the simpler projects I have had to do. However I have had a requirement to use an ORM.

I was looking forward to using the upcoming Entity component that was being built but after readingĀ  this at nabble. I decided that it was time to investigate using Doctrine.

So after having a little play and reading all the tutorials/guides I could find I had a go @ integrating Doctrine into my latest project base.

So far no single guide/tutorial has gotten it 100% right for me. I was wanting to make it a seamless integration using the frameworks application component.

So here goes my amalgamation of quite a few guides to try and give a more complete picture. My apologies if I forget to attribute any bits of code I may have borrowed/adapted. Let me know and I will add you in.

Some things to make you aware of

  • First I built this with 1.9.5 of the framework. I havent tried it with any earlier versions but it should work pretty much the same.
  • I used Doctrine 1.2. It is still in beta on their website t the time of writing. But having had a nose through their blog I can see that they are on the verge of releasing it as stable. So by the time you read this it will most likely be stable. My reasoning for this is that 1.2 appears to handle the building of models a lot better than earlier versions.
  • In order to get all the features I wanted to use working I had to adjust part of the Doctrine library. As part of the way I wanted to use doctrine was to build models automatically from yaml files. Unfortunately Doctrine appears to be reliant upon using a component from the symfony framework. Not that I’m knocking Symfony but I much prefer Zend and as such have no desire to use Symfony. That said.. for the sake of speed I downloaded Symfony and copied the 4 files I needed. I’ll cover what I didĀ  bit later. I have logged this as a bug/task on Doctrines bug tracker (http://www.doctrine-project.org/jira/browse/DC-288) hopefully they will spot it and apply a suitable fix/inclusion.
  • All my code is part of my own library which called Zucchi. All of my code is available in my project base in my subversion repository here. feel free to make use of it.

First things first…

create your project. I use the tool so my architecturre generally follows this pattern. Once your project is created then we need to install the Doctrine library. I have seen a few different ways to install the library. To minimise the number of include paths I drop the library into my projects library folder. Obvious you may think. Not quite so, a number of guides I read suggested putting the library in its own sub-folder. I decided against this and put it straight into my library folder.

Next I need to build the folders that will contain additional data inside the project. we need to create a folder to contain some doctrine specific data. and a folder to contain our cli scripts. At the end of it we should have the following file structure

project/
    application/
        configs/
        controllers/
        doctrine/        # new doctrine folder
            data/
                fixtures/
                sql/
            migrations/
            schema/
        layouts/
        models/
        views/
    bin/        # new folder to contain cli scripts
    library/
        Doctrine/
        Doctrine.php
    public/

Quite a few folders there. A lot of guides specify a scripts folder. I use a folder called bin as I tend to store additional binary/cli scripts here historically.