0

I'm trying to search in my database with LIKE now builder is working great except 1 thing: I would like to have a % in front and back of every parameter, but the way the builder works, it places a ' in front and back of every param when it is executed. so I can't add them manually

Is there a way to do this?

this is the query:

$queryBuilder = $this->db->createQueryBuilder();
$queryBuilder
    ->select('t.id', 't.status', 't.title', 't.price', 'u.username')
    ->from('tools', 't')
    ->innerJoin('t', 'key_for_tools', 'kft', 'kft.tools_id = t.id')
    ->innerJoin('t', 'keywords', 'k', 'kft.key_id = k.id')
    ->innerJoin('t', 'users', 'u', 't.user_id = u.id');

for($i=0; $i < sizeof($tags); $i++){
    $queryBuilder->andWhere('k.key LIKE %' . $queryBuilder->createPositionalParameter($tags[$i]) . '%');
}
$result = $this->db->fetchAll($queryBuilder->getSql(),$tags);

but this generates a string like:

SELECT t.id, t.status, t.title, t.price, u.username FROM tools t INNER JOIN key_for_tools kft ON kft.tools_id = t.id INNER JOIN keywords k ON kft.key_id = k.id INNER JOIN users u ON t.user_id = u.id WHERE (k.key LIKE %'tag'%) but I want the % between the '

if anyone knows how to do this

Kiwi
  • 2,713
  • 7
  • 44
  • 82
  • http://stackoverflow.com/questions/3755718/doctrine2-dql-use-setparameter-with-wildcard-when-doing-a-like-comparison should help you – VMai May 20 '14 at 11:16

2 Answers2

0

As @VMAI said, this fixed my problem

    for($i=0; $i < sizeof($tags); $i++){
        $queryBuilder->andWhere($queryBuilder->expr()->like('k.key', $queryBuilder->expr()->literal('%' . $tags[$i] . '%')));
    }
Kiwi
  • 2,713
  • 7
  • 44
  • 82
0

add it to the parameter directly like:

$queryBuilder->andWhere('k.key LIKE ' . $queryBuilder->createPositionalParameter('%' . tags[$i] . '%'));

all "things" that should be between the qoutes, must be added in there

Jacob A.
  • 270
  • 3
  • 10