mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Can't escape quote in orderby with a function #7373
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @aleblanc on GitHub (May 16, 2024).
When I add a quote in a function in orderBy like this :
This code generate this error :
NB: If I remove the quote in the string the query works.
Environnement :
"name": "gedmo/doctrine-extensions", "version": "v3.15.0", "name": "doctrine/orm", "version": "2.19.5",@greg0ire commented on GitHub (May 16, 2024):
You are using a DBAL connection method to quote a string that you insert in an ORM query. That seems wrong.
@aleblanc commented on GitHub (May 16, 2024):
I have the same error if I escape the quote manually whitout DBAL connection method :
$queryBuilder->orderBy('INSTR(field, \'string with quote \\\' that make error \')');@greg0ire commented on GitHub (May 16, 2024):
Have you tried using a prepared statement instead?
@aleblanc commented on GitHub (May 16, 2024):
No, I can't use prepare in my use case, but I think it would work without queryBuilder.
I have tried also with setParameter but I have have this error :
Maybe the problem come from Lexer because if I do that, that work :
$queryBuilder->orderBy('INSTR(field, \'string with quote that make error \')');Maybe Lexer seems to consider \' as the end of a string.
@greg0ire commented on GitHub (May 16, 2024):
By prepared statement I meant a query that uses parameters. It's IMO better than attempting to do the escaping yourself.
You must be doing something wrong, because I have tried modifying
3d9af3187f/tests/Tests/ORM/Functional/OneToOneInverseSideLoadAfterDqlQueryTest.php (L45)so that it uses
inverse'aas a parameter, and it seems to parse just fine.@aleblanc commented on GitHub (May 16, 2024):
Your exemple don't utilise a function, in my exemple I use a function INSTR in the orderby :
$queryBuilder->orderBy('INSTR(field, :value )')->setParameter('value', 'string with quote that make error');
@greg0ire commented on GitHub (May 16, 2024):
Well I just tried this:
It parses fine as well. I also tried
That parses just right as well.
@aleblanc commented on GitHub (May 16, 2024):
Indeed with the setParameters, it works, I had a problem in my loop (who remove the orderby parameter), thanks for the help