yapro / doctrine-ext
Doctrine 扩展
v2.0
2024-08-02 10:36 UTC
Requires
- php: >=8.0
- doctrine/orm: *
- yapro/helpers: *
Requires (Dev)
- ext-pdo_sqlite: *
- doctrine/annotations: *
- phpmd/phpmd: @stable
- symfony/cache: *
- symfony/phpunit-bridge: *
- symfony/serializer: *
This package is not auto-updated.
Last update: 2024-09-25 11:37:08 UTC
README
这是一个解决常见 Doctrine 问题的库。
安装
在您的 composer.json 文件中添加要求或运行
composer require yapro/doctrine-ext
作为开发者依赖
composer require apro/doctrine-ext dev-master
内容概览
- 数组到 Doctrine 实体注水器
- BigIntType - 用于原生 bigint 支持
- ReloadDatabaseTrait - 用于实体测试/数据测试
- EntityShouldToInvokeParentConstructTest - 验证扩展其他类的实体测试
- EntityAutoFillTimeListener - 用于自动填充任何实体的字段(如 createdAt, updatedAt)
- ImportedObjectInterface - 用于特定实体的自动填充字段(如 createdAt, updatedAt)
- RequiredFieldsTrait - 用于没有自动生成 ID 的实体(字段:createdAt, updatedAt)
- AutoIdAndRequiredFieldsTrait - 用于具有自动生成 ID 的实体(扩展 RequiredFieldsTrait)
数组到 Doctrine 实体注水器
您可以使用数组填充这个 Doctrine 实体对象,例如
$data = [ 'name' => 'Fred Jones', 'email' => 'fred@example.com', 'company' => 2, 'permissions' => [1, 2, 3, 4] ]; $hydrator = new \YaPro\DoctrineExt\Hydrator\ArrayHydrator($entityManager); $entity = $hydrator->hydrate('App\Entity\User', $data);
您甚至可以使用 JSON API 资源数据填充用户( 文档 )
$data = [ 'attributes' => [ 'name' => 'Fred Jones', 'email' => 'fred@example.com', ], 'relationships' => [ 'company' => [ 'data' => ['id' => 1, 'type' => 'company'], ], 'permissions' => [ 'data' => [ ['id' => 1, 'type' => 'permission'], ['id' => 2, 'type' => 'permission'], ['id' => 3, 'type' => 'permission'], ['id' => 4, 'type' => 'permission'], ['name' => 'New permission'] ] ] ] ]; $hydrator = new \YaPro\DoctrineExt\Hydrator\JsonApiHydrator($entityManager); $entity = $hydrator->hydrate('App\Entity\User', $data);
或者这样
$json = '{ "parentId": 12, "title": "title1", "comments": [{"parentId": 23, "message": "str1"}, {"parentId": 34, "message": "str2"}] }'; $hydrator = new \YaPro\DoctrineExt\Hydrator\SimpleHydrator($entityManager, new \YaPro\Helper\JsonHelper()); $entity = $hydrator->fromJson(Article::class, $json);
见 更多示例
注意:移除 pmill/doctrine-array-hydrator 依赖后,不支持 Doctrine ORM v2。
ReloadDatabaseTrait
ReloadDatabaseTrait 使用示例
use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use YaPro\DoctrineExt\ReloadDatabaseTrait; class ExampleClassTest extends KernelTestCase { use ReloadDatabaseTrait; protected static EntityManagerInterface $entityManager; public static function setUpBeforeClass() { self::$entityManager = self::$container->get(EntityManagerInterface::class); } public function myTest() { $this->truncateClass('My\User'); $this->truncateTable('user_orders'); $this->truncateAllTables(); // ... some useful actions } }
BigIntType
BigIntType - 原生 php bigint 支持,配置示例
doctrine: dbal: types: bigint: YaPro\DoctrineExt\DbalType\BigIntType
和用法
<?php namespace App\Entity; class MyEntity { #[ORM\Column(type: Types::BIGINT)] private int $mybigint = 0;
DBALConnectionWrapper
简单重复查询以获取总行数的示例
doctrine: dbal: default_connection: my_connection connections: my_connection: wrapper_class: YaPro\DoctrineExt\Wrapping\DBALConnectionWrapper host: '%env(MYSQL_HOST)%' port: '%env(MYSQL_PORT)%' dbname: '%env(MYSQL_DATABASE)%' user: '%env(MYSQL_USERNAME)%' password: '%env(MYSQL_PASSWORD)%' driver: 'pdo_mysql' server_version: '5'
用法
$items = $this->getEntityManager()->getConnection()->fetchAll(" SELECT id, title, createdAt FROM Article WHERE isShow = 1 ORDER BY createdAt DESC LIMIT 20, 10 "); // get the total number of items like: SELECT COUNT(*) FROM Article WHERE isShow = 1 echo $this->getEntityManager()->getConnection()->fetchColumn(DBALConnectionWrapper::SELECT_FOUND_ROWS); // if you use TotalItemsTrait you can call: echo $this->getTotalItems();
EntityAutoFillTimeListener 配置示例
YaPro\DoctrineExt\EventListener\EntityAutoFillTimeListener: tags: - { name: doctrine.event_listener, event: prePersist } - { name: doctrine.event_listener, event: preUpdate }
开发者
docker build -t yapro/doctrine-ext:latest -f ./Dockerfile ./ docker run --rm --user=$(id -u):$(id -g) --add-host=host.docker.internal:host-gateway -it --rm -v $(pwd):/app -w /app yapro/doctrine-ext:latest bash cp -f composer.lock.php8 composer.lock composer install -o
调试测试
PHP_IDE_CONFIG="serverName=common" \ XDEBUG_SESSION=common \ XDEBUG_MODE=debug \ XDEBUG_CONFIG="max_nesting_level=200 client_port=9003 client_host=host.docker.internal" \ vendor/bin/simple-phpunit --cache-result-file=/tmp/phpunit.cache -v --stderr --stop-on-incomplete --stop-on-defect \ --stop-on-failure --stop-on-warning --fail-on-warning --stop-on-risky --fail-on-risky
Cs-Fixer
wget https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/releases/download/v3.61.1/php-cs-fixer.phar && chmod +x ./php-cs-fixer.phar
./php-cs-fixer.phar fix --config=.php-cs-fixer.dist.php -v --using-cache=no --allow-risky=yes
更新 phpmd 规则
wget https://github.com/phpmd/phpmd/releases/download/2.12.0/phpmd.phar && chmod +x ./phpmd.phar /app/vendor/phpmd/phpmd/src/bin/phpmd . text phpmd.xml --exclude .github/workflows,vendor --strict --generate-baseline