ordermind/doctrine-manager-bundle

Symfony bundle that provides communicative managers for models

1.1.0 2017-04-17 11:16 UTC

README

68747470733a2f2f7472617669732d63692e6f72672f4f726465726d696e642f646f637472696e652d6d616e616765722d62756e646c652e7376673f6272616e63683d6d6173746572 (Please read this note on why tests currently fail)

doctrine-manager-bundle

This is a Symfony bundle that provides managers for repositories and entities/documents (henceforth referred to as models) in order to facilitate container awareness and convenience methods as well as events. It also encourages a good application structure in accordance with this blog post.

Getting started

Compatibility

This bundle has been tested on PHP 5.4 and higher as well as HHVM. Both Symfony 2.8 and Symfony 3.x are supported. For Doctrine both ORM and ODM (with MongoDB) are supported, however currently there is an issue with Doctrine's MongoDB dependencies which makes the ODM tests fail on PHP 7 and HHVM. Read more here.

Installation

1. Install with composer

composer require ordermind/doctrine-manager-bundle

2. Add the bundle to app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Ordermind\DoctrineManagerBundle\OrdermindDoctrineManagerBundle(),
        // ...
    );
}

Configuration

Create services for your repository managers

You should do this for every model that you have.

ORM example:

# file: app/config/services.yml
services:
    my_repository_manager:
        class: Ordermind\DoctrineManagerBundle\Services\Manager\RepositoryManager
        factory: ['@ordermind_doctrine_manager.service.repository_manager_factory', getRepositoryManager]
        arguments:
            - AppBundle\Entity\MyEntity

ODM example:

# file: app/config/services.yml
services:
    my_repository_manager:
        class: Ordermind\DoctrineManagerBundle\Services\Manager\RepositoryManager
        factory: ['@ordermind_doctrine_manager.service.repository_manager_factory', getRepositoryManager]
        arguments:
            - AppBundle\Document\MyDocument

Usage

1. Inject the repository manager instead of Doctrine's entity/document manager

Example from dependency injection (recommended):

# file: app/config/services.yml
services:
    my_service:
        class: AppBundle\Services\MyService
        arguments:
            - ['@my_repository_manager']

Example from controller (discouraged):

$repositoryManager = $this->get('my_repository_manager');

2. Create a model manager instead of creating an entity/document

When the first two steps are done, you have full access to the repository manager. You can use this just like a regular repository but it also provides some convenience methods which are detailed in the API documentation. You also get access to a number of events both in the repository manager and the model manager that allow you to interrupt or modify communications with the repository and model, respectively.

The repository manager will in almost all cases return model managers instead of models in the result. It also has the capability of creating a new model manager. There are two main ways of doing this:

Use RepositoryManager::create() to create a completely new model instance with the model manager

Example:

$modelManager = $repositoryManager->create();

Example with constructor parameters:

$modelManager = $repositoryManager->create($arg1, $arg2);
Use RepositoryManager::wrapModel() to create a manager from an existing model

Example:

$modelManager = $repositoryManager->wrapModel($model);

3. Use the managers as proxies for all calls to the repository and model

By treating the model manager as an enhanced model and the repository manager as an enhanced repository and always using them instead of calling the repository or model directly, you will be able to maximize the benefits of this bundle. They will then be able to fire events that allow you to control if and how the calls are passed on to the underlying repository/model. For more information, please consult the API documentation.

Instead of doing this:

$result = $repository->findAll(); //Returns all models in this repository

Do this:

$result = $repositoryManager->findAll(); //Returns all models in this repository wrapped by modelManagers and allows you to tamper with the result before it is returned.

Similarly, instead of doing this:

$value = $model->getMyProperty(); //Returns the value of $myProperty in $model

Do this:

$value = $modelManager->getMyProperty(); //Still returns the value of $myProperty in $model but allows you to abort the call before it is passed on to the model.

API Documentation

  • Services overview
  • Events overview NOTE: This didn't come out so great due to phpdoc issues. When in doubt, please check the interface file for the event you're interested in for complete documentation.