Skip to content

Commit f367ec9

Browse files
committed
Document FunctionsBuilder::stringAgg()
Adds documentation for the new portable stringAgg() method, covering per-driver translation and supported ordering.
1 parent 7f1f801 commit f367ec9

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

docs/en/appendices/5-4-migration-guide.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ version is reported as `unknown`), the header is omitted.
9898
See [Query Builder](../orm/query-builder#advanced-conditions).
9999
- Added `inOrNull()` and `notInOrNull()` methods for combining `IN` conditions with `IS NULL`.
100100
- Added `isDistinctFrom()` and `isNotDistinctFrom()` methods for null-safe comparisons.
101+
- Added `FunctionsBuilder::stringAgg()` for portable string aggregation.
102+
Translates to `STRING_AGG` or `GROUP_CONCAT` per driver.
103+
See [Query Builder](../orm/query-builder#string-aggregation).
101104
- Added PostgreSQL index access method reflection. Non-btree indexes (`gin`,
102105
`gist`, `spgist`, `brin`, `hash`) are now reflected with an `accessMethod`
103106
field and regenerated with the correct `USING` clause. The `Index` class

docs/en/orm/query-builder.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ Calculate the max of a column. `Assumes arguments are literal values.`
360360
`count()`
361361
Calculate the count. `Assumes arguments are literal values.`
362362

363+
`stringAgg()`
364+
Aggregate string values using a separator. Translates to `STRING_AGG()`,
365+
`GROUP_CONCAT()`, or `LISTAGG()` depending on the database driver.
366+
`Assumes the first argument is a literal value.`
367+
363368
`cast()`
364369
Convert a field or expression from one data type to another.
365370

@@ -471,6 +476,38 @@ FROM articles;
471476
> [!NOTE]
472477
> Use `func()` to pass untrusted user data to any SQL function.
473478
479+
#### String Aggregation
480+
481+
The `stringAgg()` method provides a portable way to aggregate string values
482+
using a separator. It translates to the appropriate native SQL function for
483+
each driver (`STRING_AGG()` on PostgreSQL and SQL Server, `GROUP_CONCAT()` on
484+
MySQL, and `STRING_AGG()` or `GROUP_CONCAT()` on MariaDB/SQLite depending on
485+
version):
486+
487+
```php
488+
$query = $articles->find();
489+
$query->select([
490+
'category_id',
491+
'titles' => $query->func()->stringAgg('title', ', '),
492+
])
493+
->groupBy('category_id');
494+
```
495+
496+
You can optionally specify an ordering for the aggregated values via the
497+
third argument:
498+
499+
```php
500+
$query->func()->stringAgg('title', ', ', ['title' => 'ASC']);
501+
```
502+
503+
`STRING_AGG` with aggregate-local ordering is supported on PostgreSQL,
504+
SQL Server, MariaDB 10.5+ and SQLite 3.44+. MySQL translates the call to
505+
`GROUP_CONCAT` in all cases.
506+
507+
::: info Added in version 5.4.0
508+
`FunctionsBuilder::stringAgg()` was added.
509+
:::
510+
474511
### Ordering Results
475512

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

0 commit comments

Comments
 (0)