Skip to content

Commit 03891db

Browse files
author
Cristoforo Cervino
committed
Merge branch 'release/1.0.0' into main
2 parents f42047c + 3a10e0e commit 03891db

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

README.md

+24-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Shared Query Builder
44

5-
#### Doctrine 2 query builder decorator - [AndanteProject](https://github.com/andanteproject)
5+
#### Doctrine 2 [Query Builder](https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/reference/query-builder.html) decorator - [AndanteProject](https://github.com/andanteproject)
66

77
[![Latest Version](https://img.shields.io/github/release/andanteproject/shared-query-builder.svg)](https://github.com/andanteproject/shared-query-builder/releases)
88
![Github actions](https://github.com/andanteproject/shared-query-builder/actions/workflows/workflow.yml/badge.svg?branch=main)
@@ -47,7 +47,7 @@ $ composer require andanteproject/shared-query-builder
4747

4848
After creating
4949
your [query builder](https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/reference/query-builder.html), wrap
50-
it inside out SharedQueryBuilder.
50+
it inside our `SharedQueryBuilder`.
5151

5252
```php
5353
use Andante\Doctrine\ORM\SharedQueryBuilder;
@@ -83,7 +83,7 @@ $qb = $sqb->unwrap();
8383

8484
### Entity methods
8585

86-
You can ask the SharedQueryBuilder if it has and entity in the `from` statement or some `join` statements.
86+
You can ask the `SharedQueryBuilder` if it has and entity in the `from` statement or some `join` statements.
8787

8888
```php
8989
if($sqb->hasEntity(User::class)) // bool returned
@@ -99,15 +99,15 @@ statement or a `join` statement).
9999
$userAlias = $sqb->getAliasForEntity(User::class); // string 'u' returned
100100
```
101101

102-
You can use `withAlias` method to smoothly add a condition for that entity:
102+
You can use `withAlias` method to smoothly add a condition for that entity property:
103103

104104
```php
105105
if($sqb->hasEntity(User::class)) // bool returned
106106
{
107107
$sqb
108108
->andWhere(
109109
$sqb->expr()->eq(
110-
$sqb->withAlias(User::class, 'email'),
110+
$sqb->withAlias(User::class, 'email'), // string 'u.email'
111111
':email_value'
112112
)
113113
)
@@ -138,10 +138,12 @@ All query builder `join` methods can be used as usually, but you can also use th
138138
$sqb->join(/* args */);
139139
$sqb->innerJoin(/* args */);
140140
$sqb->leftJoin(/* args */);
141+
141142
// Lazy join methods
142143
$sqb->lazyJoin(/* args */);
143144
$sqb->lazyInnerJoin(/* args */);
144145
$sqb->lazyLeftJoin(/* args */);
146+
145147
// They works with all the ways you know you can perform joins in Doctrine
146148
// A: $sqb->lazyJoin('u.address', 'a')
147149
// or B: $sqb->lazyJoin('Address::class', 'a', Expr\Join::WITH, $sqb->expr()->eq('u.address','a'))
@@ -150,13 +152,14 @@ $sqb->lazyLeftJoin(/* args */);
150152
By doing this, you are defining a `join` statement **without actually adding it** to your DQL query. It is going to be
151153
added to your DQL query only when you add **another condition/dql part** which refers to it. Automagically ✨.
152154

153-
Based on how much confused you are right now, you can check for [you do should you need this](#why-do-i-need-this)
154-
reasons or [some examples](#examples) to achieve your "OMG" moment.
155+
Based on how much confused you are right now, you can check for [why you should need this](#why-do-i-need-this) or [some examples](#examples) to achieve your "OMG" revelation moment.
155156

156157
## Examples
157158

158159
Let's suppose we need to list `User` entities but we also have an **optional filter** to search an user by it's
159-
address `Building` name. There is no need to perform any join until we decide to use that filter. We can use **Laxy
160+
address `Building` name.
161+
162+
There is no need to perform any join until we decide to use that filter. We can use **Laxy
160163
Join** to achieve this.
161164

162165
```php
@@ -195,7 +198,7 @@ $users = $sqb->getQuery()->getResults();
195198
// AND b.name = 'Building A'
196199
```
197200

198-
You are probably thinking: **why don't we achieve the same result like this**? (keep in mind that avoid to perform
201+
You are probably thinking: **why don't we achieve the same result with the following, more common, way**? (keep in mind that avoid to perform
199202
unecessary joins is still a requirement)
200203

201204
```php
@@ -207,6 +210,7 @@ $qb
207210
$qb->expr()->eq('u.verifiedEmail', ':verified_email')
208211
)
209212
->setParameter('verified_email', true);
213+
210214
if(!empty($buildingNameFilter)){
211215
$qb
212216
->lazyJoin('u.address', 'a')
@@ -217,6 +221,7 @@ if(!empty($buildingNameFilter)){
217221
->setParameter('building_name_value', $buildingNameFilter)
218222
;
219223
}
224+
220225
$users = $qb->getQuery()->getResults(); // Same result as example shown before
221226
// But this has some down sides further explained
222227
```
@@ -225,9 +230,9 @@ The code above is perfectly fine if you build this whole query in the **same con
225230

226231
- 👍🏻 You are *aware* of the whole query building process;
227232
- 👍🏻 You are *aware* of which entities are involved;
228-
- 👍🏻 You are *aware* of which alias are defined for each entity;
233+
- 👍🏻 You are *aware* of which alias are defined for each entity.
229234

230-
The problems are:
235+
But you have problems:
231236

232237
- 👎🏻 You are mixing query structure definition with optional filtering criteria.
233238
- 👎🏻 Code is is quickly going to be an unreadable mess.
@@ -291,9 +296,9 @@ class BuildingNameFilter implements FilterInterface
291296

292297
**We are committing some multiple sins here! 💀 The context is changed.**
293298

294-
- 👎🏻 You are *not aware* of the whole query building process. Is the given QueryBuilder even a query on Users?;
299+
- 👎🏻 You are *not aware* of the whole query building process. Is the given QueryBuilder even a query on User entity?;
295300
- 👎🏻 You are *not aware* of which entities are involved. Which entities are already been joined?;
296-
- 👎🏻 You are *not aware* of which alias are defined for each entity. No way we are calling `u.address` by convention
301+
- 👎🏻 You are *not aware* of which aliases are defined for each entity. No way we are calling `u.address` by convention
297302
🤨;
298303
- 👎🏻 Our job in this context is just to apply some filter. We *can* change the query by adding some join statements
299304
but we *should avoid* that. What if another filter also need to perform those joins? Devastating. 😵
@@ -367,11 +372,13 @@ class BuildingNameFilter implements FilterInterface
367372
```
368373

369374
- 👍🏻 No extra join statements executed when there is no need for them;
370-
- 👍🏻 We can discover if the Query Builder is handling Building entities too and then apply our filtering logic;
371-
- 👍🏻 We are not guessing for entity aliases;
372-
- 👍🏻 Our filter class is only responsible of filtering;
375+
- 👍🏻 We can discover if the Query Builder is handling an Entity and then apply our business logic;
376+
- 👍🏻 We are not guessing entity aliases;
377+
- 👍🏻 Our filter class is only responsible for filtering;
373378
- 👍🏻 There can be multiple filter class handling different criteria on the same entity without having duplicated join
374379
statements;
375-
- The world is an happier place 💁.
380+
- The world is a happier place 💁.
381+
382+
Give us a ⭐️ if your world is now a happier place too! 💃🏻
376383

377384
Built with love ❤️ by [AndanteProject](https://github.com/andanteproject) team.

0 commit comments

Comments
 (0)