ray / doctrine-orm-module
Ray.Di 的 doctrine/orm 模块
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
为 Doctrine ORM 提供的 Ray.Di 模块
安装
Composer 安装
$ 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:password@127.0.0.1:5432/myapp_db' ]; $this->install(new EntityManagerModule($params, $entityDir)); } }
了解数据库连接配置的更多信息 (点击此处)。
DI 特性
- EntityManagerInject 为
Doctrine\ORM\EntityManagerInterface
接口
事务管理
首先安装 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); // <-- } }
任何带有 @Transactional
标记的类中的方法都会在事务中执行。
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->...; } }
带有 @Transactional
标记的方法将在事务中执行。
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->...; } }
生成代理类(用于生产环境)
代理类可以提高生产环境中的性能。如果您绑定了 ProxyDir
,则代理类将在第一次使用时自动生成到目录中。
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)); } }
了解有关 代理对象 的更多信息。
记录查询
如果您想记录查询,您还需要绑定 Psr\Log\LoggerInterface
并安装 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