Skip to content

Commit

Permalink
allow to add columns to entity fetchers
Browse files Browse the repository at this point in the history
  • Loading branch information
tflori committed Jul 11, 2021
1 parent 0a9c181 commit 310c586
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
17 changes: 15 additions & 2 deletions docs/entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ entities from the query.
In fact `EntityFetcher` extends the `QueryBuilder` and for more information about building queries please have a look at
the [QueryBuilder](querybuilder.md) documentation.

Please note that the `EntityFetcher` does not allow fetching other columns than the columns from the main table of
the class, is always distinct and restricts to change the fetch mode. Example usages:
Example usages:

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

Please note that it is not possible to remove the predefined modifier `DISTINCT` and the column selection `t0.*` (the
entity table is aliased `t0`). However it is possible to add additional columns (for example aggregates from joined
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
to update the entity in the DB with these extra columns.

Example with aggregate column:
```php
$albums = $entityManager->fetch(Album::class)
->joinRelated('images')
->groupBy('t0.id')
->column('COUNT(images.id)', [], 'imageCount')
->all();
```

### Set and get columns

Every column is available by magic getter and using the column naming previously described in documentation about
Expand Down
7 changes: 0 additions & 7 deletions src/EntityFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,6 @@ public function columns(array $columns = null)
return $this;
}

/** @return $this
* @internal */
public function column($column, $args = [], $alias = '')
{
return $this;
}

/** @return $this
* @internal */
public function setFetchMode($mode, $classNameObject = null, array $ctorargs = null)
Expand Down
11 changes: 10 additions & 1 deletion tests/EntityFetcher/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,20 @@ public function columnsCantBeChanged()
{
$fetcher = $this->em->fetch(ContactPhone::class);
$fetcher->columns(['a', 'b']);
$fetcher->column('c');

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

/** @test */
public function columnsCanBeAdded()
{
$fetcher = $this->em->fetch(ContactPhone::class);
$fetcher->column('(a + b)', [], 'aPlusB');

self::assertSame('SELECT DISTINCT t0.*,("t0"."a" + "t0"."b") AS aPlusB ' .
'FROM "contact_phone" AS t0', $fetcher->getQuery());
}

/** @test */
public function fetchModeCantBeChanged()
{
Expand Down

0 comments on commit 310c586

Please sign in to comment.