Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework 2 + Doctrine 2 [closed]

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!

like image 755
flux Avatar asked Oct 23 '11 15:10

flux


1 Answers

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.

like image 52
Samuel Herzog Avatar answered Sep 22 '22 18:09

Samuel Herzog