Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/en/appendices/5-4-migration-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ version is reported as `unknown`), the header is omitted.
See [Query Builder](../orm/query-builder#advanced-conditions).
- Added `inOrNull()` and `notInOrNull()` methods for combining `IN` conditions with `IS NULL`.
- Added `isDistinctFrom()` and `isNotDistinctFrom()` methods for null-safe comparisons.
- Added `FunctionsBuilder::stringAgg()` for portable string aggregation.
Translates to `STRING_AGG` or `GROUP_CONCAT` per driver.
See [Query Builder](../orm/query-builder#string-aggregation).
- Added PostgreSQL index access method reflection. Non-btree indexes (`gin`,
`gist`, `spgist`, `brin`, `hash`) are now reflected with an `accessMethod`
field and regenerated with the correct `USING` clause. The `Index` class
Expand Down
37 changes: 37 additions & 0 deletions docs/en/orm/query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ Calculate the max of a column. `Assumes arguments are literal values.`
`count()`
Calculate the count. `Assumes arguments are literal values.`

`stringAgg()`
Aggregate string values using a separator. Translates to `STRING_AGG()`,
`GROUP_CONCAT()`, or `LISTAGG()` depending on the database driver.
`Assumes the first argument is a literal value.`

`cast()`
Convert a field or expression from one data type to another.

Expand Down Expand Up @@ -471,6 +476,38 @@ FROM articles;
> [!NOTE]
> Use `func()` to pass untrusted user data to any SQL function.

#### String Aggregation

The `stringAgg()` method provides a portable way to aggregate string values
using a separator. It translates to the appropriate native SQL function for
each driver (`STRING_AGG()` on PostgreSQL and SQL Server, `GROUP_CONCAT()` on
MySQL, and `STRING_AGG()` or `GROUP_CONCAT()` on MariaDB/SQLite depending on
version):

```php
$query = $articles->find();
$query->select([
'category_id',
'titles' => $query->func()->stringAgg('title', ', '),
])
->groupBy('category_id');
```

You can optionally specify an ordering for the aggregated values via the
third argument:

```php
$query->func()->stringAgg('title', ', ', ['title' => 'ASC']);
```

`STRING_AGG` with aggregate-local ordering is supported on PostgreSQL,
SQL Server, MariaDB 10.5+ and SQLite 3.44+. MySQL translates the call to
`GROUP_CONCAT` in all cases.

::: info Added in version 5.4.0
`FunctionsBuilder::stringAgg()` was added.
:::

### Ordering Results

To apply ordering, you can use the `orderBy()` method:
Expand Down