Zend Framework 2 + Doctrine 2

I would like to start development with the Zend Framework, and I would like to use zf2. Since I'm using Doctrine 2, can you offer some tutorials to help me integrate it in zf2? Thank!

+42
php zend-framework zend-framework2 doctrine doctrine2
Oct 23 '11 at 15:48
source share
2 answers

Last checked: ZF2.2. *, DoctrineORMModule 0.7.

Official module

You can download DoctrineORMModule through the composer:

  • add doctrine/doctrine-orm-module to your composer.json request (sample code after bcs list of formatting problems)
  • run php composer.phar install
  • create a ./data/DoctrineORMModule/Proxy directory and provide write access for your application
  • configure the doctrine through your applications /config/autoload to provide the module with project-specific settings (database, etc.)
  • configure the display of the essence of the doctrine in your config.php modules
  • add an object to your project
  • add DoctrineORMModule and DoctrineModule to config/application.config.php
  • run the cli tool to create your tables ./vendor/bin/doctrine-module orm:schema-tool:create

I strongly discourage you from using the composer as this is an easy way to install dependencies and configure all autoloaders. ZF2 is also sent through the composer by default.

Code example

composer.json

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

settings noun

 <?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 getcha to keep abreast of: the paths to your entites must be fully qualified. It is best to start with __DIR__ , otherwise everything will break (every new project I wonder why the command line tool does not 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 module.

Further reading:

Marco Pivetta did , which I recommend to everyone who uses this module.

Jason Grimes wrote a tutorial presented at phpdeveloper.org, which should help establish the doctrine before the official module appeared.

+53
Feb 01 '12 at 7:21
source share

Update:

I explored this a bit further, and it looks like Doctrine 2 support in Zend Framework 2 is still working. I recommend following this Github Gist to control how progress is progressing.

In the meantime, you can check out the zf2-doctrine-provider repository from Michiel Staessen. There is no documentation yet, but that would be the logical next step as soon as you can speed up the Bisna library in ZF 1.x.

I am pleased to see that most of the discussion around ZF2 for Doctrine support has focused on flexibility. It is my hope (and apparently others) that using Doctrine in the Zend Framework will not be either / or a choice, but rather an option for creating specific parts of a solid and flexible domain model.

Original post:

Fortunately, most of the core resources for learning ZF 1.x + Doctrine accounted for the switch to ZF2. They do not cover official Doctrine support in ZF2, but they should help you get started with many of the basic principles.

Zendcasts has an excellent Doctrine 2 training series starting with " Unit Testing Doctrine 2 Entities ." Be sure to watch the videos that follow him as John L. (presenter) continued to incorporate best practices as he progressed through the video.

Zend Technologies has the ultimate webinar called " Zend Framework v1 + Doctrine v2 ." Facilitators specifically discuss how they structured a sample application to host ZF2 migration.

As for the native support for ZF2, I haven't found anything yet. Rob Allen, the famous ZF2 tutorial uses Zend \ Db, and Nick Belhomme does not have the Zend Framework 2.0 Cookbook .

Good luck with your work and please post any useful resources you find on ZF2 / D2.

+5
Oct 24 '11 at 4:35
source share



All Articles