A Doctrine ORM Module for Ray.Di
$ composer require ray/doctrine-orm-module
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:[email protected]:5432/myapp_db'
];
$this->install(new EntityManagerModule($params, $entityDir));
}
}
Learn more about the database connection configuration.
- EntityManagerInject for
Doctrine\ORM\EntityManagerInterface
interface
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->...;
}
}
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.
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); // <--
}
}
$ php docs/demo/run.php
// It works!
- PHP 5.6+
- hhvm