Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine2 query builder does not quote the string

Below is the code excerpt I have

$column_name = "ipAddress";
$qb = EntityManagerContainer::get()->createQueryBuilder();
$qb->select('u')
    ->from(BlacklistedIps::class, 'u');

if($search_term)
{   
    $clause = $qb->expr()->like("u.".$column_name, "'%$search_term%'");
    $qb->where($clause);
}

$query = $qb->getQuery();

$result = $query->getResult();

It works absolutely fine (although it's open to SQL injection but that's another story).

My problem with this is the need to have "'%$search_term%'". Without this extra set of single quotes the query fails

Uncaught exception 'Doctrine\ORM\Query\QueryException' with message 'SELECT u FROM Orm\Entity\BlacklistedIps u WHERE u.ipAddress LIKE %123% ORDER BY u.reason desc' in ***

I am not entirely sure I am doing it the right way. Because if I do, then there is a bug (mssing feature?) in Doctrine2. When I do

$qb->expr()->like("u.".$column_name, "%$search_term%");

then I am ABSOLUTELY sure that I am dealing with a string. When integers or booleans or floats, etc are compared to each other different operators are used, but definitely not LIKE. LIKE is used ONLY when dealing with strings, so quoting the string in DQL is exactly the only possible ->like method use case.

Please tell me I am doing something wrong. I've been using Doctrine2 for couple of days only and feel fascinated by it. But don't like strings not being quoted automatically for me.

like image 505
Vladimir Hraban Avatar asked May 01 '15 11:05

Vladimir Hraban


People also ask

What is a QueryBuilder in doctrine 2?

The QueryBuilder — Doctrine 2 ORM 2 documentation 15. The QueryBuilder ¶ A QueryBuilder provides an API that is designed for conditionally constructing a DQL query in several steps. It provides a set of classes and methods that is able to programmatically build queries, and also provides a fluent API.

How to bind parameters to a query in doctrine?

Doctrine supports dynamic binding of parameters to your query, similar to preparing queries. You can use both strings and numbers as placeholders, although both have a slightly different syntax. Additionally, you must make your choice: Mixing both styles is not allowed. Binding parameters can simply be achieved as follows:

Can you use placeholders in Doctrine Query?

Binding parameters to your query ¶ Doctrine supports dynamic binding of parameters to your query, similar to preparing queries. You can use both strings and numbers as placeholders, although both have a slightly different syntax. Additionally, you must make your choice: Mixing both styles is not allowed.

Is it possible to set query hints in a QueryBuilder?

The QueryBuilder is a builder object only, it has no means of actually executing the Query. Additionally a set of parameters such as query hints cannot be set on the QueryBuilder itself. This is why you always have to convert a querybuilder instance into a Query object:


1 Answers

it looks like a problem of how you use querybuilder. You should do something like that :

$qb ->where($qb->expr()->orX($qb->expr()->like('u.'.$column_name, $qb->expr()->literal("%$searchTerm%"))))  

or

$qb->where($qb->expr()->like("u.".$column_name, array("%$searchTerm%")));  

also to avoid sql injection, a good practice is to not pass user input in any querybuilder methods, use setParameter with ? or : instead.

$qb->where('u.'.$column_name.' LIKE :searchTerm')
$qb->setParameter('searchTerm', '%'.$searchTerm.'%')  

or something like :

$qb->expr()->like('u.'.$column_name, '?1')
$qb->getQuery()->setParameter(1, '%' . $searchTerm . '%');
like image 67
scraaappy Avatar answered Oct 18 '22 19:10

scraaappy