File tree Expand file tree Collapse file tree 3 files changed +25
-10
lines changed Expand file tree Collapse file tree 3 files changed +25
-10
lines changed Original file line number Diff line number Diff line change @@ -62,8 +62,7 @@ entities from the query.
62
62
In fact ` EntityFetcher ` extends the ` QueryBuilder ` and for more information about building queries please have a look at
63
63
the [ QueryBuilder] ( querybuilder.md ) documentation.
64
64
65
- Please note that the ` EntityFetcher ` does not allow fetching other columns than the columns from the main table of
66
- the class, is always distinct and restricts to change the fetch mode. Example usages:
65
+ Example usages:
67
66
68
67
``` php
69
68
// fetch a user by $email
@@ -74,6 +73,20 @@ $articles = $entityManager->fetch(Article::class)
74
73
->all();
75
74
```
76
75
76
+ Please note that it is not possible to remove the predefined modifier ` DISTINCT ` and the column selection ` t0.* ` (the
77
+ entity table is aliased ` t0 ` ). However it is possible to add additional columns (for example aggregates from joined
78
+ tables) but keep in mind that you then maybe also need to group by the primary key of ` t0 ` . Also it will not be possible
79
+ to update the entity in the DB with these extra columns.
80
+
81
+ Example with aggregate column:
82
+ ``` php
83
+ $albums = $entityManager->fetch(Album::class)
84
+ ->joinRelated('images')
85
+ ->groupBy('t0.id')
86
+ ->column('COUNT(images.id)', [], 'imageCount')
87
+ ->all();
88
+ ```
89
+
77
90
### Set and get columns
78
91
79
92
Every column is available by magic getter and using the column naming previously described in documentation about
Original file line number Diff line number Diff line change @@ -214,13 +214,6 @@ public function columns(array $columns = null)
214
214
return $ this ;
215
215
}
216
216
217
- /** @return $this
218
- * @internal */
219
- public function column ($ column , $ args = [], $ alias = '' )
220
- {
221
- return $ this ;
222
- }
223
-
224
217
/** @return $this
225
218
* @internal */
226
219
public function setFetchMode ($ mode , $ classNameObject = null , array $ ctorargs = null )
Original file line number Diff line number Diff line change @@ -374,11 +374,20 @@ public function columnsCantBeChanged()
374
374
{
375
375
$ fetcher = $ this ->em ->fetch (ContactPhone::class);
376
376
$ fetcher ->columns (['a ' , 'b ' ]);
377
- $ fetcher ->column ('c ' );
378
377
379
378
self ::assertSame ('SELECT DISTINCT t0.* FROM "contact_phone" AS t0 ' , $ fetcher ->getQuery ());
380
379
}
381
380
381
+ /** @test */
382
+ public function columnsCanBeAdded ()
383
+ {
384
+ $ fetcher = $ this ->em ->fetch (ContactPhone::class);
385
+ $ fetcher ->column ('(a + b) ' , [], 'aPlusB ' );
386
+
387
+ self ::assertSame ('SELECT DISTINCT t0.*,("t0"."a" + "t0"."b") AS aPlusB ' .
388
+ 'FROM "contact_phone" AS t0 ' , $ fetcher ->getQuery ());
389
+ }
390
+
382
391
/** @test */
383
392
public function fetchModeCantBeChanged ()
384
393
{
You can’t perform that action at this time.
0 commit comments