demos-europe/edt-dql
Composer 安装命令:
composer require demos-europe/edt-dql
包简介
Extension for demos-europe/edt-queries to use Doctrine ORM as data source.
README 文档
README
Extends edt/queries with support for Doctrine ORM.
Also functions as helper library for Doctrine ORM to generate DQL queries. Currently, the focus lies in the detection of join clauses from a given property path to sort and filter the result set.
A basic understanding of the Doctrine ORM by the user is assumed for this library to be used.
Example
Suppose we have a Book, Person and Address entity.
A Book has an author property which references Person entity.
A Person has a birth property containing information about the birth location and date.
An Birth entity has a country property which is a string.
The goal is to get all Book entities of which the author was born in the USA.
This could be done by first fetching all entities from the database
and filter the result in PHP. But for performance and memory reasons
it makes sense to execute the filter in the database.
With DQL we could create a query with two joins to get the desired result:
use \Tests\data\DqlModel\Book; $queryBuilder = $this->getEntityManager()->createQueryBuilder() ->select('Book') ->from(Book::class, 'Book') ->leftJoin('Book.author', 'Person') ->leftJoin('Person.birth', 'Birth') ->where('Birth.country = :countryName') ->setParameter('countryName', 'USA');
This library can generate a similar query builder from the following code:
use \Tests\data\DqlModel\Book; use EDT\DqlQuerying\ConditionFactories\DqlConditionFactory; use \EDT\DqlQuerying\Utilities\QueryBuilderPreparer; /** @var \Doctrine\ORM\EntityManager $entityManager */ $entityManager = $this->getEntityManager(); $conditionFactory = new DqlConditionFactory(); $metadataFactory = $entityManager->getMetadataFactory(); $builderPreparer = new QueryBuilderPreparer(Book::class, $metadataFactory, new JoinFinder($metadataFactory)); $builderPreparer->setWhereExpressions([ $conditionFactory->propertyHasValue('USA', 'authors', 'birth', 'country'), ]); $builderPreparer->fillQueryBuilder($entityManager->createQueryBuilder());
The main advantage of the second version does not lie in line count or readability but in the removal of the need to manually specify the query in detail. This allows dynamically receiving queries and directly executing them in the database (provided authorization and validation is checked beforehand).
Conditions
Beside the PropertyHasValue shown above more condition types are supported.
All conditions provided by the library can be found and created using the DqlConditionFactory class.
If the provided conditions do not suffice you can write you own clauses by implementing ClauseInterface.
Condition Nesting
Conditions can be grouped together using AND or OR conjunctions:
$conditionFactory = new EDT\DqlQuerying\ConditionFactories\DqlConditionFactory(); $andConditions = [ /* ... */]; $orConditions = [ /* ... */]; $orCondition = $conditionFactory->anyConditionApplies(...$orConditions); $nestedCondition = $conditionFactory->allConditionsApply(...$andConditions);
Sorting
Defining the sorting can be done similarly as defining conditions. In the following example books will be sorted by their authors name as first priority and the authors birthdate as second priority.
use EDT\DqlQuerying\ConditionFactories\DqlConditionFactory; use EDT\DqlQuerying\SortMethodFactories\SortMethodFactory; use \Tests\data\DqlModel\Book; use EDT\DqlQuerying\Utilities\QueryBuilderPreparer; /** @var \Doctrine\ORM\EntityManager $entityManager */ $entityManager = $this->getEntityManager(); $conditionFactory = new DqlConditionFactory(); $sortingFactory = new SortMethodFactory(); $metadataFactory = $entityManager->getMetadataFactory(); $builderPreparer = new QueryBuilderPreparer(Book::class, $metadataFactory, new JoinFinder($metadataFactory)); $builderPreparer->setSelectExpressions([ $sortingFactory->propertyAscending('authors', 'name'), $sortingFactory->propertyDescending('authors', 'birthdate'), ]); $builderPreparer->fillQueryBuilder($entityManager->createQueryBuilder());
If the provided sort implementations do not suffice you can write you own sort implementation.
Credits and acknowledgements
Conception and implementation by Christian Dressler with many thanks to eFrane.
demos-europe/edt-dql 适用场景与选型建议
demos-europe/edt-dql 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.97k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 06 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 demos-europe/edt-dql 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 demos-europe/edt-dql 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 25.97k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 39
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-23