ray/doctrine-orm-module

doctrine/orm module for Ray.Di

1.5.0 2016-08-31 23:26 UTC

This package is not auto-updated.

Last update: 2024-11-06 23:52:34 UTC


README

Scrutinizer Code Quality Code Coverage Build Status Packagist Packagist

A Doctrine ORM Module for Ray.Di

Installation

Composer install

$ composer require ray/doctrine-orm-module

Module install

use Ray\Di\AbstractModule;
use Ray\DoctrineOrmModule\EntityManagerModule;

class AppModule extends AbstractModule
{
    protected function configure()
    {
        $entityDir = '/path/to/Entity/';

        $params = [
            'driver'   => 'pdo_pgsql',
            'user'     => 'username',
            'password' => 'password',
            'host'     => '127.0.0.1'
            'port'     => '5432',
            'dbname'   => 'myapp_db'
        ];

        $this->install(new EntityManagerModule($params, $entityDir));

        //// OR ////

        $params = [
            'url' => 'postgresql://username:password@127.0.0.1:5432/myapp_db'
        ];

        $this->install(new EntityManagerModule($params, $entityDir));
    }
}

Learn more about the database connection configuration.

DI trait

Transaction management

First, install TransactionalModule.

use Ray\Di\AbstractModule;
use Ray\DoctrineOrmModule\EntityManagerModule;
use Ray\DoctrineOrmModule\TransactionalModule;

class AppModule extends AbstractModule
{
    protected function configure()
    {
        $this->install(new EntityManagerModule($params, $entityDir));

        $this->install(new TransactionalModule); // <--
    }
}

Any method in the class marked with @Transactional is executed in a transaction.

use Ray\DoctrineOrmModule\Annotation\Transactional;
use Ray\DoctrineOrmModule\EntityManagerInject;

/**
 * @Transactional
 */
class UserService
{
    use EntityManagerInject;

    public function foo()
    {
        // transaction is active
        $this->entityManager->...;
    }

    public function bar()
    {
        // transaction is active
        $this->entityManager->...;
    }
}

The method marked with @Transactional is executed in a transaction.

use Ray\DoctrineOrmModule\Annotation\Transactional;
use Ray\DoctrineOrmModule\EntityManagerInject;

class UserService
{
    use EntityManagerInject;

    /**
     * @Transactional
     */
    public function foo()
    {
        // transaction is active
        $this->entityManager->...;
    }

    public function bar()
    {
        // transaction is not active
        $this->entityManager->...;
    }
}

Generating Proxy classes (for production)

Proxy classes improve the performance in a production environment. If you bind ProxyDir, proxy classes are automatically generated into the directory when they are used the first time.

use Ray\Di\AbstractModule;
use Ray\DoctrineOrmModule\Annotation\ProxyDir;
use Ray\DoctrineOrmModule\EntityManagerModule;

class AppModule extends AbstractModule
{
    protected function configure()
    {
        $this->bind()->annotatedWith(ProxyDir::class)->toInstance('/path/to/proxy'); // <--

        $this->install(new EntityManagerModule($params, $entityDir));
    }
}

Learn more about the Proxy Object.

Logging queries

If you want to log queries, you additionally need to bind Psr\Log\LoggerInterface and install SqlLoggerModule.

use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Ray\Di\AbstractModule;
use Ray\DoctrineOrmModule\EntityManagerModule;
use Ray\DoctrineOrmModule\SqlLoggerModule;
use Ray\DoctrineOrmModule\TransactionalModule;

class AppModule extends AbstractModule
{
    protected function configure()
    {
        $this->install(new EntityManagerModule($params, $entityDir));
        $this->install(new TransactionalModule);

        $this->bind(LoggerInterface::class)->toInstance(new Logger('myapp')); // <--
        $this->install(new SqlLoggerModule); // <--
    }
}

Demo

$ php docs/demo/run.php
// It works!

Requirements

  • PHP 5.6+
  • hhvm