PartialSearchFilter: ESCAPE '\' breaks multi-value queries on PostgreSQL with PHP < 8.4 (HY093)
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 75/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- php, postgresql
- Domain
- backend-api-design, databases
Research direction
Start by locating PartialSearchFilter and its formatLikeValue() method, then inspect the filter tests for multi-value queries and LIKE ... ESCAPE behavior. Reproduce the reported case with PostgreSQL on PHP 8.3 and check the existing behavior on PHP 8.4. Done means multi-value partial searches work on affected PostgreSQL versions without breaking escaping of % and _ or other supported databases.
Written by the indexing model from the issue text.
Description
API Platform version(s) affected: 4.4.3
(Doctrine ORM 3.5.0, Doctrine DBAL 3.9.5, PHP 8.3.33 with pdo_pgsql, PostgreSQL 17. The ESCAPE '\' clause is identical on the 4.4, 5.0 and main branches.)
Description
With PostgreSQL on PHP < 8.4, PartialSearchFilter breaks as soon as a bound parameter follows its LIKE ... ESCAPE '\' clause. The most visible case is a multi-value query (?name[]=foo&name[]=bar): the collection request fails with
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
(first thrown by the pagination count query, Doctrine\ORM\Tools\Pagination\Paginator::count()). A single value works, because its only placeholder comes before the escape literal.
This is related to #8434 (Oracle), but the cause is different. Here the generated SQL is correct, and DBAL's own SQL parser also sees both placeholders:
WHERE f0_.name LIKE ? ESCAPE '\' OR f0_.name LIKE ? ESCAPE '\'
(Connection::quote('\') returns '\' on pdo_pgsql; standard_conforming_strings is on.)
The failure comes from PDO itself. Before PHP 8.4, PDO's generic placeholder scanner treats a backslash inside a quoted string as an escape character, so '\' is read as an unterminated string and every ? after it is ignored. PHP 8.4 introduced driver-specific SQL parsers, and the same query works there.
How to reproduce
Plain PDO, no API Platform or Doctrine involved:
$pdo = new PDO('pgsql:host=...;dbname=...', $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
// OK: single placeholder, before the escape literal
$pdo->prepare("SELECT 'abc' LIKE ? ESCAPE '\\'")->execute(['%b%']);
// PHP 8.3.33: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
// PHP 8.4.26: OK
$pdo->prepare("SELECT 'abc' LIKE ? ESCAPE '\\' OR 'abc' LIKE ? ESCAPE '\\'")->execute(['%b%', '%z%']);
// OK on PHP 8.3: no escape clause, or another escape character
$pdo->prepare("SELECT 'abc' LIKE ? OR 'abc' LIKE ?")->execute(['%b%', '%z%']);
$pdo->prepare("SELECT 'abc' LIKE ? ESCAPE '!' OR 'abc' LIKE ? ESCAPE '!'")->execute(['%b%', '%z%']);
With API Platform:
#[ApiResource(operations: [
new GetCollection(parameters: [
'name' => new QueryParameter(filter: new PartialSearchFilter()),
]),
])]
#[ORM\Entity]
class Book { /* ... string $name ... */ }
GET /books?name=foo → 200
GET /books?name[]=foo&name[]=bar → 500 (HY093)
The legacy SearchFilter with the partial strategy works with the same request, since it emits no ESCAPE clause. So migrating from #[ApiFilter(SearchFilter::class)] to PartialSearchFilter, as the 4.4 deprecation suggests, introduces this regression on PostgreSQL with PHP < 8.4.
Possible Solution
Use an escape character that needs no backslash, for example ESCAPE '!', and escape !, % and _ with it in formatLikeValue(). That avoids this PDO issue, and per #8434 it also suits Oracle. I only verified ESCAPE '!' on PostgreSQL (snippet above).
Additional Context
Verified with the same PostgreSQL 17 server and the same PDO snippet on PHP 8.3.33 (fails) and PHP 8.4.26 (passes). As a workaround, we use a custom FilterInterface implementation that emits LIKE without an ESCAPE clause.
- Dominant language
- PHP
- Stars
- 2.6k
- Forks
- 987
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 84
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from api-platform/core
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
api-platform/core#8660 ·
Maintainers usually reply within 1 day
-
DeserializeProvider calls PartialDenormalizationException::getErrors(), deprecated in Symfony 8.1Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
api-platform/core#8650 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
api-platform/core#8649 ·
Maintainers usually reply within 1 day
-
`OrderExtension` and `OrderFilter` pass string sort directions, deprecated since `doctrine/orm` 3.7Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
api-platform/core#8648 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
api-platform/core#8647 ·
Maintainers usually reply within 1 day
All issues in api-platform/core
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
crazy-goat/rabbit-stream#830 ·
Maintainers usually reply within 1 day
-
sync-en
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
sync-en
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 4 days
-
Перевод устарел
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
Combination form: image thumbnails collapse to 0×0 when a stylesheet sets `img { max-width: 100% }`Open
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
PrestaShop/PrestaShop#43200 ·
Maintainers usually reply within 1 day