Skip to content

Commit 57513e8

Browse files
authored
Merge pull request #8279 from cakephp/docs-string-agg
Document FunctionsBuilder::stringAgg()
2 parents 5d904e0 + 2772f7e commit 57513e8

2 files changed

Lines changed: 48 additions & 8 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ version is reported as `unknown`), the header is omitted.
110110
See [Query Builder](../orm/query-builder#advanced-conditions).
111111
- Added `inOrNull()` and `notInOrNull()` methods for combining `IN` conditions with `IS NULL`.
112112
- Added `isDistinctFrom()` and `isNotDistinctFrom()` methods for null-safe comparisons.
113+
- Added `FunctionsBuilder::stringAgg()` for portable string aggregation.
114+
Translates to `STRING_AGG` or `GROUP_CONCAT` per driver.
115+
See [Query Builder](../orm/query-builder#string-aggregation).
113116
- Added `Connection::afterCommit()` to register callbacks that run after the
114117
outermost transaction commits. Callbacks are discarded on rollback.
115118
See [Database Basics](../orm/database-basics#aftercommit) for more details.

docs/en/orm/query-builder.md

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -346,31 +346,36 @@ You can access existing wrappers for several SQL functions through `SelectQuery:
346346
Generate a random value between 0 and 1 via SQL.
347347

348348
`sum()`
349-
Calculate a sum. `Assumes arguments are literal values.`
349+
Calculate a sum. *Assumes arguments are literal values.*
350350

351351
`avg()`
352-
Calculate an average. `Assumes arguments are literal values.`
352+
Calculate an average. *Assumes arguments are literal values.*
353353

354354
`min()`
355-
Calculate the min of a column. `Assumes arguments are literal values.`
355+
Calculate the min of a column. *Assumes arguments are literal values.*
356356

357357
`max()`
358-
Calculate the max of a column. `Assumes arguments are literal values.`
358+
Calculate the max of a column. *Assumes arguments are literal values.*
359359

360360
`count()`
361-
Calculate the count. `Assumes arguments are literal values.`
361+
Calculate the count. *Assumes arguments are literal values.*
362+
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.*
362367

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

366371
`concat()`
367-
Concatenate two values together. `Assumes arguments are bound parameters.`
372+
Concatenate two values together. *Assumes arguments are bound parameters.*
368373

369374
`coalesce()`
370-
Coalesce values. `Assumes arguments are bound parameters.`
375+
Coalesce values. *Assumes arguments are bound parameters.*
371376

372377
`dateDiff()`
373-
Get the difference between two dates/times. `Assumes arguments are bound parameters.`
378+
Get the difference between two dates/times. *Assumes arguments are bound parameters.*
374379

375380
`now()`
376381
Defaults to returning date and time, but accepts 'time' or 'date' to return only
@@ -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)