Emulate the field function in SQLIte by using an string search, instead of our PHP function callback

This commit is contained in:
Jan Böhmer 2023-07-29 16:50:47 +02:00
parent 62b1e33616
commit 80ed064cd6

View file

@ -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;
}
}