Setting up Doctrine with Zend Framework 2

For this post I am working though Jason Grimes’s article http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/, which in turn is based on Rob Allen’s Album tutorial. The goal is to set up The Doctrine ORM module in Zend Framework 2, and get it working with a simple example.

Getting set up

Once Rob’s tutorial is complete and we have a working application, Jason tells us to add doctrineORM to our composer.json file. To get this working I had to set the version requirement to 0.*. Also, since several packages are still in development, the minimum stability requirement has to be lowered to alpha. This last point makes me hesitant about Doctrine and ZF2 as a production solution.

After a bit of trial and error, my working composer.json looks like this:

[javascript]
{
"name": "zendframework/skeleton-application",
"description": "Skeleton Application for ZF2",
"license": "BSD-3-Clause",
"keywords": [
"framework",
"zf2"
],
"minimum-stability": "alpha",
"homepage": "http://framework.zend.com/",
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "2.*",
"doctrine/doctrine-orm-module": "0.*"
}
[/javascript]

I’ve left in the Zend Skeleton meta data for now to keep the file looking as close to the original as possible. Once composer runs, we see suggestions to also include YAML and Zend-Framework_Tools packages, but I’m going to skip those for now.

Database connection

When adding the Doctrine database connection details, Jason puts the whole config into configs/autoload/local.php. This is not the best practise as the local file should not be distributed. I have split my definitions over the global.php and local.php files to reduce the amount of config work that has to be repeated when someone else clones the project.

[php]
‘service_manager’ => array(
‘factories’ => array(
‘ZendDbAdapterAdapter’
=> ‘ZendDbAdapterAdapterServiceFactory’,
),
),
‘doctrine’ => array(
‘connection’ => array(
‘orm_default’ => array(
‘driverClass’ => ‘DoctrineDBALDriverPDOMySqlDriver’,
‘params’ => array(
‘host’ => ‘localhost’,
‘port’ => ‘3306’,
‘dbname’ => ‘album_doctrine’,
)
)
)
),
);
[/php]

 

[php]
‘doctrine’ => array(
‘connection’ => array(
‘orm_default’ => array(
‘params’ => array(
‘user’ => ‘USERNAME’,
‘password’ => ‘PASSWORD’,
)
)
)
),
);
[/php]

Just one more thing…

After this Jason’s tutorial worked great for me, with one last exception. In his deleteAction controller method, he returns an array representation of the album entity to the view, which breaks the object notation used in the view to retrieve the album’s properties. I have omitted the array conversion, which makes my deleteAction look like this:

[php]
public function deleteAction()
{
$id = (int) $this->params()->fromRoute(‘id’, 0);
if (!$id) {
return $this->redirect()->toRoute(‘album’);
}

$request = $this->getRequest();
if ($request->isPost()) {
$del = $request->getPost(‘del’, ‘No’);

if ($del == ‘Yes’) {
$id = (int) $request->getPost(‘id’);
$album = $this->getEntityManager()->find(‘AlbumEntityAlbum’, $id);
if ($album) {
$this->getEntityManager()->remove($album);
$this->getEntityManager()->flush();
}
}

// Redirect to list of albums
return $this->redirect()->toRoute(‘album’);
}

return array(
‘id’ => $id,
‘album’ => $this->getEntityManager()->find(‘AlbumEntityAlbum’, $id)
);
}
[/php]

The end

I”ve enjoyed this, my first foray into Doctrine ORM. I’m now looking forward to developing this concept further into territory where Zend’s DbTable package becomes inadequate, like table relationships.

The source for this post is available on Github: https://github.com/shanethehat/zf2-doctrine-tutorial

One comment

  • Nag
    22nd December 2014 - 9:40 am | Permalink

    From above ,Delete action working but from original Edit action is also not working for me.

    public function editAction()
    {
    $id = (int) $this->params()->fromRoute('id', 0);
    if (!$id) {
    return $this->redirect()->toRoute('album', array(
    'action' => 'add'
    ));
    }
    $album = $this->getEntityManager()->find('AlbumEntityAlbum', $id);
    $form = new AlbumForm();
    $form->bind($album);
    $form->get('submit')->setAttribute('value', 'Edit');
    $request = $this->getRequest();
    if ($request->isPost()) {
    $form->setInputFilter($album->getInputFilter());
    $form->setData($request->getPost());
    if ($form->isValid()) {
    $form->bindValues();
    $this->getEntityManager()->flush();
    // Redirect to list of albums
    return $this->redirect()->toRoute('album');
    }
    }
    return array(
    'id' => $id,
    'form' => $form,
    );
    }

    Thanks
    Nag

  • Leave a Reply

    Your email address will not be published. Required fields are marked *