ray / doctrine-orm-module
doctrine/orm module for Ray.Di
1.5.0
2016-08-31 23:26 UTC
Requires
- php: >=5.6
- doctrine/orm: ~2.5
- psr/log: ~1.0
- ray/di: ~2.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ~1.11
- phpmd/phpmd: ~2.4
- phpunit/phpunit: ~5.4
- squizlabs/php_codesniffer: ~2.6
README
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
- EntityManagerInject for
Doctrine\ORM\EntityManagerInterface
interface
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