stargazer-team / yii-doctrine
Yii3集成Doctrine扩展
2.0.0
2024-08-10 15:36 UTC
Requires
- php: >=8.1.0
- doctrine/migrations: ^3.8.0
- doctrine/orm: ^3.2.1
- psr/cache: ^2.0|^3.0
- psr/log: ^2|^3
- symfony/cache: ^6.4.7
- yiisoft/aliases: ^3.0
- yiisoft/config: ^1.5
- yiisoft/definitions: ^3.3
- yiisoft/injector: ^1.2
- yiisoft/yii-console: ^2.2.0
Requires (Dev)
- maglnet/composer-require-checker: 4.7.1
- rector/rector: ^0.15.25
- roave/infection-static-analysis-plugin: ^1.29
- vimeo/psalm: ^4.30|^5.9
This package is not auto-updated.
Last update: 2024-09-21 16:13:39 UTC
README
Yii3 Doctrine扩展
安装
安装此扩展的首选方式是通过composer
composer require stargazer-team/yii-doctrine
基本用法
配置参数 doctrine: dbal, orm, migrations, fixture 示例配置路径 example.php
DBAL
创建数据库
php yii doctrine:database:create
删除数据库
php yii doctrine:database:drop --if-exists --force
动态创建连接
<?php declare(strict_types=1); use Yiisoft\Yii\Doctrine\Dbal\Enum\ConfigOptions; use Yiisoft\Yii\Doctrine\Dbal\Factory\DynamicConnectionFactory; use Yiisoft\Yii\Doctrine\DoctrineManager; final class ConnectionService { public function __construct( private readonly DynamicConnectionFactory $dynamicConnectionFactory, private readonly DoctrineManager $doctrineManager, ) { } public function create(): void { $connectionModel = $this->dynamicConnectionFactory->createConnection( [ ConfigOptions::PARAMS => [ 'driver' => 'pdo_pgsql', 'dbname' => 'dbname', 'host' => 'localhost', 'password' => 'secret', 'user' => 'postgres', ] ], 'postgres' ); } public function close(): void { $this->doctrineManager->closeConnection('postgres'); } }
命令
- doctrine:dbal:run-sql
- doctrine:database:create
- doctrine:database:drop
ORM
如果需要默认实体管理器,请在di.php中添加
<?php declare(strict_types=1); use Yiisoft\Yii\Doctrine\DoctrineManager use Yiisoft\Yii\Doctrine\Orm\Enum\ConfigOptions; EntityManagerInterface::class => fn( DoctrineManager $doctrineManager ): EntityManagerInterface => $doctrineManager->getManager( $params['yiisoft/yii-doctrine'][ConfigOptions::ORM][ConfigOptions::DEFAULT_ENTITY_MANAGER] ?? DoctrineManager::DEFAULT_ENTITY_MANAGER, ),
使用默认实体管理器
<?php declare(strict_types=1); use Doctrine\ORM\EntityManagerInterface; final class TestController { public function __construct( private readonly EntityManagerInterface $entityManager, ) { } }
如果有两个或更多实体管理器使用Yiisoft\Yii\Doctrine\Doctrine\DoctrineManager,通过名称查找实体管理器
<?php declare(strict_types=1); use Yiisoft\Yii\Doctrine\DoctrineManager; final class Test2Controller { public function __construct( private readonly DoctrineManager $doctrineManager, ) { } }
动态创建实体管理器
<?php declare(strict_types=1); use Yiisoft\Yii\Doctrine\DoctrineManager; use Yiisoft\Yii\Doctrine\Orm\Enum\ConfigOptions; use Yiisoft\Yii\Doctrine\Orm\Factory\DynamicEntityManagerFactory; final class EntityManagerService { public function __construct( private readonly DynamicEntityManagerFactory $dynamicEntityManagerFactory, private readonly DoctrineManager $doctrineManager, ) { } public function create(): void { $this->dynamicEntityManagerFactory->create( [ ConfigOptions::CONNECTION => 'mysql', ConfigOptions::MAPPINGS => [ 'Mysql' => [ ConfigOptions::MAPPING_DIR => '@common/Mysql', ConfigOptions::MAPPING_DRIVER => DriverMappingEnum::ATTRIBUTE_MAPPING, ConfigOptions::MAPPING_NAMESPACE => 'Common\Mysql', ], ], ], [ ConfigOptions::PROXY_NAMESPACE => 'Proxies', ConfigOptions::PROXY_PATH => '@runtime/cache/doctrine/proxy', ConfigOptions::PROXY_AUTO_GENERATE => true ], 'mysql' ); $entityManager = $this->doctrineManager->getManager('mysql'); } public function reset(): void { $this->doctrineManager->resetManager('mysql'); } public function close(): void { $this->doctrineManager->closeManager('mysql'); } }
命令
- doctrine:orm:info
- doctrine:orm:generate-proxies
- doctrine:orm:mapping-describe
- doctrine:orm:run-dql
- doctrine:orm:validate-schema
- doctrine:orm:schema-tool:create
- doctrine:orm:schema-tool:drop
- doctrine:orm:schema-tool:update
缓存
扩展使用psr6缓存实现symfony cache。默认缓存Symfony\Component\Cache\Adapter\NullAdapter。
选项在params.php上添加配置
<?php declare(strict_types=1); use Yiisoft\Yii\Doctrine\Cache\CacheCollector; use Yiisoft\Yii\Doctrine\Cache\Enum\ConfigOptions; return [ 'yiisoft/yii-doctrine' => [ // Used symfony cache ConfigOptions::CACHE => [ ConfigOptions::DRIVER => CacheAdapterEnum::ARRAY_ADAPTER, // only redis or memcached ConfigOptions::SERVER => [ ConfigOptions::HOST => 'localhost', ConfigOptions::PORT => 6379 ], ConfigOptions::NAMESPACE => 'doctrine_', // only file cache driver ConfigOptions::PATH => '@runtime/cache/doctrine', ], ];
在di.php上添加配置psr-6缓存
<?php declare(strict_types=1); use Symfony\Component\Cache\Adapter\ArrayAdapter; use Yiisoft\Yii\Doctrine\Cache\CacheCollector return [ CacheCollector::DOCTRINE_HYDRATION_CACHE => fn(CacheFactory $cacheFactory): CacheItemPoolInterface => $cacheFactory->create( $params['yiisoft/yii-doctrine']['cache'] ?? [] ), // or add di.php customer implementation CacheCollector::DOCTRINE_HYDRATION_CACHE => ArrayAdapter(), CacheCollector::DOCTRINE_METADATA_CACHE => ArrayAdapter(), CacheCollector::DOCTRINE_QUERY_CACHE => ArrayAdapter(), CacheCollector::DOCTRINE_RESULT_CACHE => ArrayAdapter(), ];
命令
- doctrine:orm:clear-cache:metadata
- doctrine:orm:clear-cache:query
- doctrine:orm:clear-cache:result
迁移
创建迁移
php yii doctrine:migrations:diff
多配置
php yii doctrine:migrations:diff --configuration=mysql
迁移
php yii doctrine:migrations:migrate
命令
- doctrine:migrations:current
- doctrine:migrations:diff
- doctrine:migrations:dump-schema
- doctrine:migrations:execute
- doctrine:migrations:generate
- doctrine:migrations:latest
- doctrine:migrations:list
- doctrine:migrations:migrate
- doctrine:migrations:rollup
- doctrine:migrations:status
- doctrine:migrations:sync-metadata-storage
- doctrine:migrations:up-to-date
- doctrine:migrations:version
固定
如果需要固定,请安装包
composer require stargazer-team/yii-doctrine-fixture --dev