Code Monkey home page Code Monkey logo

doctrineormmodule's Introduction

DoctrineORM Module for Zend Framework 2

Master: Build Status

The DoctrineORMModule module intends to integrate Doctrine 2 ORM with Zend Framework 2 quickly and easily. The following features are intended to work out of the box:

  • Doctrine ORM support
  • Multiple ORM entity managers
  • Multiple DBAL connections
  • Support reuse existing PDO connections in DBAL

Requirements

Zend Framework 2

Installation

Installation of this module uses composer. For composer documentation, please refer to getcomposer.org.

Installation steps

  1. cd my/project/directory

  2. create a composer.json file with following contents (minimum stability is required since the module still has frequent updates):

    {
        "minimum-stability": "alpha",
        "require": {
            "doctrine/doctrine-orm-module": "0.*"
        }
    }
  3. run php composer.phar install

  4. open my/project/directory/config/application.config.php and add DoctrineModule and DoctrineORMModule to your modules

  5. create directory my/project/directory/data/DoctrineORMModule/Proxy and make sure your application has write access to it. This directory can be changed using the module options.

Installation steps (without composer)

  1. install DoctrineModule
  2. clone this module to vendor/DoctrineORMModule
  3. setup PSR-0 autoloading for namespace DoctrineORMModule (the directory where the classes in this namespace live is vendor/DoctrineORMModule/src/DoctrineORMModule.
  4. The module depends on Doctrine ORM 2.3.*, Doctrine DBAL 2.3.*, Doctrine Migrations. You have to download/install those packages and have them autoloaded.
  5. open my/project/directory/config/application.config.php and add DoctrineModule and DoctrineORMModule to your modules
  6. create directory my/project/directory/data/DoctrineORMModule/Proxy and make sure your application has write access to it. This directory can be changed using the module options.

Registering drivers with the DriverChain

To register drivers with Doctrine module simply add the drivers to the doctrine.driver key in your configuration.

<?php
return array(
    'doctrine' => array(
        'driver' => array(
            'my_annotation_driver' => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => array('path/to/my/entities', 'another/path/if/i/want')
            )
        )
    )
);

By default, this module ships with a DriverChain so that modules can add their entities to the chain. Once you have setup your driver you should add it to the chain as follows:

<?php
return array(
    'doctrine' => array(
        'driver' => array(
            'orm_default' => array(
                'drivers' => array(
                    'My\Namespace' => 'my_annotation_driver'
                )
            )
        )
    )
);

You also have access to the chain directly via the doctrine.driver.orm_default service and you can manipulate the chain however you wish and/or add drivers to it directly without using the driver factory and configuration array. A good place to do this is the onBootstrap() method of your Module.php file or in another service.

Setting up your connection

Setup your connection by adding the module configuration to any valid ZF2 config file. This can be any file in autoload/ or a module configuration (such as the Application/config/module.config.php file).

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

You can add more connections by adding additional keys to the connection and specifying your parameters.

Full configuration options

An exhaustive list of configuration options can be found directly in the Options classes of each module.

Usage

Registered Services

  • doctrine.connection.orm_default
  • doctrine.configuration.orm_default
  • doctrine.driver.orm_default
  • doctrine.entitymanager.orm_default
  • doctrine.eventmanager.orm_default

Command Line

Access the Doctrine command line as following

./vendor/bin/doctrine-module

Service Locator

Access the entity manager using the following alias:

<?php
$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');

Injection

You can also inject the EntityManager directly in your controllers/services by using a controller factory. Please refer to the official ServiceManager documentation for more information.

Example implementation using Boostrap event that you can add into you module class:

<?php

namespace MyModule;

class Module
{
    //..

    public function onBootstrap(\Zend\EventManager\EventInterface $e)
    {
        $application = $e->getApplication();
        $serviceManager = $application->getServiceManager();

        $controllerLoader = $serviceManager->get('ControllerLoader');

        // Add initializer to Controller Service Manager that check if controllers needs entity manager injection
        $controllerLoader->addInitializer(function ($instance) use ($serviceManager) {
            if (method_exists($instance, 'setEntityManager')) {
                $instance->setEntityManager($serviceManager->get('doctrine.entitymanager.orm_default'));
            }
        });
    }
}

Implementing EntityManagerAware interface

Coming soon!

doctrineormmodule's People

Contributors

adamlundrigan avatar bakura10 avatar basz avatar bostjanob avatar bsahlhof avatar chrvadala avatar davidwindell avatar drdev avatar evandotpro avatar guilhermeblanco avatar maks3w avatar ocramius avatar rwoverdijk avatar slamdunk avatar superdweebie avatar tobre6 avatar volkan avatar xoob avatar zluiten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.