Zend Framework 2 + Doctrine 2 [closed]

2019-01-29 22:05发布

问题:

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!

回答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.



回答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.