Skip to content

Commit 310c586

Browse files
committed
allow to add columns to entity fetchers
1 parent 0a9c181 commit 310c586

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

docs/entities.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ entities from the query.
6262
In fact `EntityFetcher` extends the `QueryBuilder` and for more information about building queries please have a look at
6363
the [QueryBuilder](querybuilder.md) documentation.
6464

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:
6766

6867
```php
6968
// fetch a user by $email
@@ -74,6 +73,20 @@ $articles = $entityManager->fetch(Article::class)
7473
->all();
7574
```
7675

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+
7790
### Set and get columns
7891

7992
Every column is available by magic getter and using the column naming previously described in documentation about

src/EntityFetcher.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,6 @@ public function columns(array $columns = null)
214214
return $this;
215215
}
216216

217-
/** @return $this
218-
* @internal */
219-
public function column($column, $args = [], $alias = '')
220-
{
221-
return $this;
222-
}
223-
224217
/** @return $this
225218
* @internal */
226219
public function setFetchMode($mode, $classNameObject = null, array $ctorargs = null)

tests/EntityFetcher/BasicTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,20 @@ public function columnsCantBeChanged()
374374
{
375375
$fetcher = $this->em->fetch(ContactPhone::class);
376376
$fetcher->columns(['a', 'b']);
377-
$fetcher->column('c');
378377

379378
self::assertSame('SELECT DISTINCT t0.* FROM "contact_phone" AS t0', $fetcher->getQuery());
380379
}
381380

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+
382391
/** @test */
383392
public function fetchModeCantBeChanged()
384393
{

0 commit comments

Comments
 (0)