demos-europe / edt-dql
Extension for demos-europe/edt-queries to use Doctrine ORM as data source.
Requires
- php: ^8.1
- demos-europe/edt-paths: ^0.26
- demos-europe/edt-queries: ^0.26
- doctrine/orm: ^2.5
- nette/php-generator: ^4.0
- symfony/cache: ^5 || ^6 || ^7
Conflicts
- demos-europe/edt-access-definitions: <0.17.1
- demos-europe/edt-extra: <0.17.1
- demos-europe/edt-jsonapi: <0.17.1
- demos-europe/edt-paths: <0.17.1
- dev-main
- 0.26.0
- 0.25.0
- 0.24.42
- 0.24.41
- 0.24.40
- 0.24.39
- 0.24.38
- 0.24.37
- 0.24.36
- 0.24.35
- 0.24.34
- 0.24.32
- 0.24.31
- 0.24.30
- 0.24.29
- 0.24.28
- 0.24.27
- 0.24.26
- 0.24.25
- 0.24.24
- 0.24.23
- 0.24.22
- 0.24.21
- 0.24.20
- 0.24.19
- 0.24.18
- 0.24.17
- 0.24.16
- 0.24.15
- 0.24.14
- 0.24.13
- 0.24.12
- 0.24.11
- 0.24.10
- 0.24.9
- 0.24.8
- 0.24.7
- 0.24.6
- 0.24.5
- 0.24.4
- 0.24.3
- 0.24.2
- 0.24.1
- 0.24.0
- 0.23.6
- 0.23.5
- 0.23.4
- 0.23.3
- 0.23.2
- 0.23.1
- 0.23.0
- 0.22.3
- 0.22.2
- 0.22.1
- 0.22.0
- 0.21.4
- 0.21.3
- 0.21.2
- 0.21.1
- 0.21.0
- 0.20.1
- 0.18.0
- 0.17.5
- 0.16.1
- 0.16.0
- 0.15.1
- 0.15.0
- 0.14.1
- 0.14.0
- 0.13.2
- 0.13.1
- 0.13.0
- 0.12.23
- 0.12.22
- 0.12.21
- 0.12.20
- 0.12.19
- 0.12.18
- 0.12.17
- 0.12.16
- 0.12.15
- 0.12.14
- 0.12.13
- 0.12.12
- 0.12.11
- 0.12.10
- 0.12.9
- 0.12.8
- 0.12.7
- 0.12.6
- 0.12.5
- 0.12.4
- 0.12.3
- 0.12.2
- 0.12.1
- 0.12.0
- 0.11.2
- 0.11.1
- 0.11.0
- 0.10.0
- 0.9.0
- 0.8.0
- 0.7.1
- 0.7.0
- 0.6.4
- 0.6.3
- 0.6.2
- 0.6.1
This package is auto-updated.
Last update: 2024-11-20 20:42:35 UTC
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.