From 80ed064cd6918ec638c03c6b7d3fd19e21ef8994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sat, 29 Jul 2023 16:50:47 +0200 Subject: [PATCH] Emulate the field function in SQLIte by using an string search, instead of our PHP function callback --- src/Doctrine/Helpers/FieldHelper.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Doctrine/Helpers/FieldHelper.php b/src/Doctrine/Helpers/FieldHelper.php index 17fbe733..357fe1fc 100644 --- a/src/Doctrine/Helpers/FieldHelper.php +++ b/src/Doctrine/Helpers/FieldHelper.php @@ -57,14 +57,20 @@ final class FieldHelper //Generate a unique key from the field_expr $key = 'field2_' . (string) $bound_param; - //Otherwise we have to it using the FIELD2 function - $qb->orderBy("FIELD2($field_expr, :$key)", $order); - $qb->setParameter($key, implode(',', $param->getValue())); + self::addSqliteOrderBy($qb, $field_expr, $key, $param->getValue(), $order); } return $qb; } + private static function addSqliteOrderBy(QueryBuilder $qb, string $field_expr, string $key, array $values, ?string $order = null): void + { + //Otherwise we emulate it using + $qb->orderBy("LOCATE(CONCAT($field_expr, ','), :$key)", $order); + //The value have to end with a comma, otherwise the search using INSTR will fail + $qb->setParameter($key, implode(',', $values) . ','); + } + /** * Add an ORDER BY FIELD expression to the query builder. The correct FIELD function is used depending on the database platform. * In this function the values are passed as an array. If you want to reuse an existing bound parameter, use the addOrderByFieldParam function. @@ -87,10 +93,11 @@ final class FieldHelper //Generate a unique key from the field_expr //Otherwise we have to it using the FIELD2 function - $qb->orderBy("FIELD2($field_expr, :$key)", $order); - $qb->setParameter($key, implode(',', $values)); + self::addSqliteOrderBy($qb, $field_expr, $key, $values, $order); } + $qb->setParameter($key, $values); + return $qb; } } \ No newline at end of file