@@ -360,6 +360,11 @@ Calculate the max of a column. `Assumes arguments are literal values.`
360360` count() `
361361Calculate 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() `
364369Convert 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
476513To apply ordering, you can use the ` orderBy() ` method:
0 commit comments