Zend Framework 2 + Doctrine 2

19,777

Solution 1

last time checked: ZF2.2.*, DoctrineORMModule 0.7.

Official Module

You may want to load DoctrineORMModule via composer:

  • add doctrine/doctrine-orm-module to your composer.json's require (example code after list bcs of formatting problems)
  • run php composer.phar install
  • create the directory ./data/DoctrineORMModule/Proxy and ensure write access for your application
  • configure doctrine via your applications /config/autoload to give the module the project-specific settings (database etc)
  • configure doctrine's entity mapping in your modules config.php
  • add an entity to your project
  • add DoctrineORMModule and DoctrineModule to your config/application.config.php
  • run the cli tool to generate your tables ./vendor/bin/doctrine-module orm:schema-tool:create

I strongly discourage you from not using composer, as it is an easy way to install dependencies and have the autoloaders all set up. Also ZF2 ships via composer by default.

Example Code

composer.json

{  
    "require" : {  
        "php": ">=5.3.3",  
        "zendframework/zendframework": "2.*"                
        "doctrine/doctrine-orm-module": "0.*"                
    }  
}  

entities settings

<?php
return array(
    'doctrine' => array(
        'driver' => array(
            // defines an annotation driver with two paths, and names it `my_annotation_driver`
            'my_annotation_driver' => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => array(
                    'path/to/my/entities',
                    'another/path'
                ),
            ),

            // default metadata driver, aggregates all other drivers into a single one.
            // Override `orm_default` only if you know what you're doing
            'orm_default' => array(
                'drivers' => array(
                    // register `my_annotation_driver` for any entity under namespace `My\Namespace`
                    'My\Namespace' => 'my_annotation_driver'
                )
            )
        )
    )
);

A gotcha to be aware of: The paths to your entites should be fully qualified. Best start with __DIR__, else things will break (Every new project I wonder why the command line tool doesn't work until I find this error ... ;)

connection settings

<?php
return array(
    'doctrine' => array(
        'connection' => array(
            // default connection name
            'orm_default' => array(
                'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
                'params' => array(
                    'host'     => 'localhost',
                    'port'     => '3306',
                    'user'     => 'username',
                    'password' => 'password',
                    'dbname'   => 'database',
                )
            )
        )
    ),
);

All code examples are part of the official doctrine module readme

Further Reading:

Marco Pivetta made a wonderful presentation about doctrine-module usage, which I recommend to everybody using this module.

Jason Grimes wrote a tutorial featured on phpdeveloper.org which should help installing doctrine, before there was an official module.

Solution 2

Update:

I researched this a little further and it sounds like native support for Doctrine 2 in Zend Framework 2 is still in the works. I recommend keeping an eye on this Github Gist to monitor how the progress is coming along.

In the meantime, you might want to check out the repository zf2-doctrine-provider from Michiel Staessen. There's no documentation for it yet, but it would be a logical next-step once you're up to speed on how the Bisna library works in ZF 1.x.

I'm encouraged to see that most of the discussion around ZF2 native support for Doctrine has been focused on flexibility. It's my hope (and apparently others too) that using Doctrine in Zend Framework won't be an either/or choice but rather an option for building specific portions of a solid and flexible domain model.

Original Post:

Fortunately most of the main resources for learning ZF 1.x + Doctrine took into account the migration to ZF2. They don't cover native support for Doctrine in ZF2, but they should help you get started with many of the main principles.

Zendcasts has an excellent Doctrine 2 tutorial series starting with "Unit Testing Doctrine 2 Entities." Be sure to watch the videos that follow it also, as Jon L. (the presenter) continued to incorporate best practices as he progressed through the videos.

Zend Technologies has a definitive webinar titled "Zend Framework v1 + Doctrine v2". The presenters specifically discuss how they structured the sample application to accommodate the ZF2 migration.

As for ZF2 native support, I haven't found anything yet either. Rob Allen's well-known ZF2 tutorial uses Zend\Db and there's no Doctrine coverage (yet) in Nick Belhomme's "Zend Framework 2.0 Cookbook."

Good luck getting started and please post any good resources you find on ZF2/D2.

Share:
19,777
flux
Author by

flux

Student of Computer Science, web developer

Updated on June 02, 2022

Comments

  • flux
    flux almost 2 years

    I would like to start developing with Zend Framework and I would like to use zf2. Since I use Doctrine 2, can you suggest some tutorials to help me to integrate it in zf2? Thanks!

  • Kees Schepers
    Kees Schepers over 12 years
    The Webinar from Jon. L. is really great. It helped me too integrating Doctrine2 and Zend Framework 1.10.11. BUT you should be aware that the Bisna 'glue' from 'Nolasnowbal' only works with Doctrine 2.0. If you want to integrate Doctrine 2.1. in a Zend Framework project you should use this fork: github.com/guilhermeblanco/ZendFramework1-Doctrine2 if you want help with the new version by an example just reply :)
  • cantera
    cantera over 12 years
    Kees is definitely correct. The post "How do I configure Bisna for use with Doctrine 2.1..." (link below) helped me a lot with making the Bisna upgrade work seamlessly.
  • cantera
    cantera over 12 years
  • flux
    flux about 12 years
    thanks... this tutorial has been very helpful!
  • markdrake
    markdrake over 11 years
    Also, in order to make the unit testing works with phpunit you should add the 'DoctrineModule', and 'DoctrineORMModule' to your modules array in your TestConfig.php file or you will get the following error: Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for doctrine.entitymanager.orm_default