Featured Post

A review of the book: Zend Framework 2 Cookbook from Packt Publishing

Review of the book: Zend Framework 2 Cookbook written by Josephus Callaars, issued by Packt Publishing. There is many cookery books although I haven’t seen yet any about Zend Framework 2. No matter how you name the book, title is just a title and every book have to stand out of the competitions....

Read More

Integrating Zend Framework 2.x with Doctrine 2.x ORM

Posted by Tomasz Kuter | Posted in Programming, Web development | Posted on 02-03-2013

Tags: , , , , , , ,


Today I would like to present Doctrine 2 ORM integration with Zend Framework 2.
I had some problems with this, so I would like to share the solution for my issues.

Downloading sample project

First of all you need to download example ZF2 project. We have two example projects created by Zend Framework team:

I propose to download first one.
If you downloaded ZIP archive from the GitHub repository, just go to extracted project directory and run command line from that location.

Downloading vendor libraries

Now try to download latest Zend Framework by typing in command line:

You shouldn’t experience any problems in that.

You can get a message from composer, that it is not up to date:

You can upgrade it by typing the command:

Downloading Doctrine 2 ORM

Now we should modify the composer configuration file to be able download latest Doctrine 2 ORM.

composer.json should look like:

Now you should update vendor libraries with added Doctrine 2 ORM module:

If you don’t have installed git, you could experience following error message:

It’s becasue in composer.json file you should used “Doctrine-ORM-Module’ instead of “DoctrineORMModule”.

Next issue is when you cannot run git command, but it is required to load git submodules:

With working and installed git (path to Git is in your PATH variable) you should be able to download Doctrine 2 ORM and related modules.

Using DoctrineORMModule

Modify script /application/application.php with following modules list:

Now lets update file /module/Application/config/module.config.php as follow:

In above example we have configuration of the Entities from two modules: Album and Users.
As you can see Album entities are stored in Album directory and Users entities in Users directory – all as it should.

Database configuration

Now it is turn for the database configuration: host, username, password and database.
You need to edit /config/autoload/local.php file with your database credentials:

Album controller setup

Then add code to the AlbumController to get the Doctrine EntityManager from the ZF service manager. Edit /module/Album/src/Album/Controller/AlbumController.php and make the following changes.

Import the EntityManager namespace:

Add a protected property and a getter method:

Now you can use $this->getEntityManager() within the AlbumController to access the Doctrine EntityManager.

If you have at least a few controllers you can create single one with above changes,
and then extend it with your controllers.

Create the Album entity

Create album table if it not exists yet.
Appropriate SQL script you will find at /module/Album/data/album.sql

Update your database with that script:

This adds new column "discs", which we use in next tutorial.

For now, you should have fully operational Doctrine Entity Manager, which is just a step before getting some data from the database.
At first we need to configure Album Entity (/module/Album/src/Album/Entity/Album.php) as follow:

The form configuration should like this:

AlbumController.php body should look as follow:

And this is it for now. In next post I will show how to separate some database operations from the controller to the model, how to use DQL (Doctrine Query Language), etc.

All the code you can download from that address:

Stay tuned.

Write a comment