Skip to content

Commit 47f2679

Browse files
committed
Update README.md
1 parent fba7068 commit 47f2679

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

README.md

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
Lean Mapper Query
22
=================
33

4-
Lean Mapper Query is a concept of a *query object* for [Lean Mapper library](https://github.com/Tharos/LeanMapper) which helps to build complex queries using automatic joins (*idea taken from [NotORM library](http://www.notorm.com/)*). Look at the [suggested base classes](https://gist.github.com/mibk/9410266). For Czech documentation have a look at the [wiki](https://github.com/mibk/LeanMapperQuery/wiki).
4+
Lean Mapper Query is a concept of a *query object* for
5+
[Lean Mapper library](https://github.com/Tharos/LeanMapper) which helps to build complex
6+
queries using automatic joins (*idea taken from [NotORM library](http://www.notorm.com/)*).
7+
Look at the [suggested base classes](https://gist.github.com/mibk/9410266). For Czech
8+
documentation have a look at the [wiki](https://github.com/mibk/LeanMapperQuery/wiki).
59

610
Features
711
--------
812

9-
- it behaves as an `SQL` preprocessor, hence most SQL expressions are available
10-
- automatic joins using *dot notation* (`@book.tags.name`)
11-
- ability to query both repositories or entities
13+
- behaves as a `SQL` preprocessor, hence most SQL expressions are available
14+
- automatic joins using the *dot notation* (`@book.tags.name`)
15+
- ability to query repositories or entities
1216
- support for implicit filters
1317

1418

@@ -25,7 +29,7 @@ composer require mbohuslavek/leanmapper-query:@dev
2529
What does it do?
2630
----------------
2731

28-
Suppose we have following repositories:
32+
Suppose we have the following repositories:
2933

3034
```php
3135
class BaseRepository extends LeanMapper\Repository
@@ -44,31 +48,31 @@ class BookRepository extends BaseRepository
4448
}
4549
```
4650

47-
and following entities:
51+
and the following entities:
4852

4953
```php
5054
/**
51-
* @property int $id
55+
* @property int $id
5256
* @property string $name
5357
*/
5458
class Tag extends LeanMapper\Entity
5559
{
5660
}
5761

5862
/**
59-
* @property int $id
60-
* @property Author $author m:hasOne
61-
* @property Tag[] $tags m:hasMany
63+
* @property int $id
64+
* @property Author $author m:hasOne
65+
* @property Tag[] $tags m:hasMany
6266
* @property DateTime $pubdate
63-
* @property string $name
64-
* @property bool $available
67+
* @property string $name
68+
* @property bool $available
6569
*/
6670
class Book extends LeanMapper\Entity
6771
{
6872
}
6973

7074
/**
71-
* @property int $id
75+
* @property int $id
7276
* @property string $name
7377
* @property Book[] $books m:belongsToMany
7478
*/
@@ -91,23 +95,27 @@ $bookRepository = new BookRepository(...);
9195
$books = $bookRepository->find($query);
9296
```
9397

94-
Database query will look like this:
98+
The database query will look like this:
99+
95100
```sql
96101
SELECT [book].*
97102
FROM [book]
98103
LEFT JOIN [author] ON [book].[author_id] = [author].[id]
99104
WHERE ([author].[name] = 'Karel')
100105
```
101106

102-
You can see it performs automatic joins via *dot notation*. It supports all types of relationships which are known to **Lean Mapper**.
107+
You can see it performs automatic joins via the *dot notation*. It supports all relationship
108+
types known to **Lean Mapper**.
103109

104110
It is very easy to use SQL functions. We can update query like this:
111+
105112
```php
106113
$query->where('DATE(@pubdate) > %d', '1998-01-01');
107114
$books = $bookRepository->find($query);
108115
```
109116

110-
and change the database query into following:
117+
which changes the database query into the following:
118+
111119
```sql
112120
SELECT [book].*
113121
FROM [book]
@@ -118,7 +126,7 @@ WHERE ([author].[name] = 'Karel') AND (DATE([book].[pubdate]) > '1998-01-01')
118126
Don't repeat yourself
119127
---------------------
120128

121-
You can extend `Query` and define own methods.
129+
You can extend the `Query` class and define your own methods.
122130

123131
```php
124132
class BookQuery extends LeanMapperQuery\Query
@@ -141,7 +149,8 @@ $books = $this->bookRepository->find($query);
141149
Querying entities
142150
-----------------
143151

144-
It is also possible to query an entity property (*currently only those properties with `BelongsToMany` or `HasMany` relationships*). Let's build `BaseEntity`:
152+
It is also possible to query an entity property (*currently only those properties with
153+
`BelongsToMany` or `HasMany` relationships*). Let's make the `BaseEntity` class:
145154

146155
```php
147156
class BaseEntity extends LeanMapperQuery\Entity
@@ -163,9 +172,10 @@ class Book extends BaseEntity
163172
}
164173
```
165174

166-
*Note that `BaseEntity` extends `LeanMapperQuery\Entity` to make the following possible.*
175+
*Note that `BaseEntity` must extend `LeanMapperQuery\Entity` to make the following possible.*
167176

168-
We have defined the `find` method as `protected` because with specifying the method name in `$magicMethodsPrefixes` property you can query entities like this:
177+
We have defined the `find` method as `protected` because by specifying the method name in the
178+
`$magicMethodsPrefixes` property, you can query entities like this:
169179

170180
```php
171181
$book; // previously fetched instance of an entity from a repository
@@ -174,7 +184,8 @@ $query->where('@name !=', 'ebook');
174184
$tags = $book->findTags($query);
175185
```
176186

177-
*The magic method `findTags` will eventually call your protected method `find` with 'tags' as the 1 argument.*
187+
*The magic method `findTags` will eventually call your protected method `find` with 'tags' as
188+
the 1st argument.*
178189

179190
The resulting database query looks like this:
180191

@@ -184,13 +195,16 @@ FROM [tag]
184195
WHERE [tag].[id] IN (1, 2) AND ([tag].[name] != 'ebook')
185196
```
186197

187-
The first condition in where clause `[tag].[id] IN (1, 2)` is taken from the entity traversing (*tags are queried against this particular book entity's own tags*).
198+
The first condition in the `where` clause, `[tag].[id] IN (1, 2)`, is taken from the entity
199+
traversing (*tags are queried against this particular book entity's own tags*).
188200

189201

190202
What else you can do?
191203
---------------------
192204

193-
If we slightly modify our `BaseRepository` and `BaseEntity` we can simplify working with query objects. *To achieve this look at the [suggested base classes](https://gist.github.com/mbohuslavek/9410266)*. It makes the following possible.
205+
If we slightly modify `BaseRepository` and `BaseEntity`, we can simplify working with query objects.
206+
*To achieve this look at the [suggested base classes](https://gist.github.com/mibk/9410266)*. It makes
207+
the following possible.
194208

195209
```php
196210
$books = $bookRepository->query()

0 commit comments

Comments
 (0)