2016-09-09 92 views
1

Hiho,學說不會綁定參數查詢

我有以下問題。 我想使用orX,andX和Like查詢構建器創建一個查詢。 查詢自我正確生成,但參數不會被綁定。

的代碼創建查詢:

$qb = $this->getEntityManager()->createQueryBuilder(); 
$qb->select('u') 
    ->from('Application\Entity\User', 'u') 
    ->andWhere(
     $qb->expr()->orx(
      $qb->expr()->andX(
       $qb->expr()->like('u.name', ':name'), 
       $qb->expr()->like('u.lastname', ':lastname') 
      ), 
      $qb->expr()->like('u.email', ':email') 
     ) 
    ) 
    ->setParameter('name', '%' . $findBy['name'] . '%') 
    ->setParameter('lastname', '%' . $findBy['lastname'] . '%') 
    ->setParameter('email', '%' . $findBy['email'] . '%'); 

getParameters轉儲()如下所示:

object(Doctrine\Common\Collections\ArrayCollection)[579] 
private 'elements' => 
    array (size=3) 
     0 => 
     object(Doctrine\ORM\Query\Parameter)[575] 
      private 'name' => string 'name' (length=4) 
      private 'value' => string '%Michael%' (length=9) 
      private 'type' => int 2 
     1 => 
     object(Doctrine\ORM\Query\Parameter)[576] 
      private 'name' => string 'lastname' (length=8) 
      private 'value' => string '%Müller%' (length=8) 
      private 'type' => int 2 
     2 => 
     object(Doctrine\ORM\Query\Parameter)[577] 
      private 'name' => string 'email' (length=5) 
      private 'value' => string '%[email protected]%' (length=20) 
      private 'type' => int 2` 

是否有身體有一個想法,爲什麼參數沒有綁定到查詢?

+0

你在說什麼參數呢? – martin

+0

用' - > setParameter('...','...')設置的參數' –

+0

它們有什麼問題? – martin

回答

0

->expr()->like(第二個參數用於LIKE()比較。您可以嘗試類似的東西:

$this->expr()->like('u.name', $this->expr()->literal('%'.$findBy['name'].'%')) 

希望這有助於

+0

因此查詢現在是正確的,但結果爲空。如果我直接在數據庫上運行查詢,我得到了一個結果。 –

+0

Hi @MatusvonMatushausen試着調試DQL轉儲'$ qb-> getQuery() - > getSQL()的結果'讓我知道值爲 – Matteo

+0

getSQL的轉儲:'SELECT u0_.email AS email_0,u0_.password AS password_1 ,u0_.lastonline AS lastonline_2,u0_.name AS name_3,u0_.lastname AS lastname_4,u0_.ip AS ip_5,u0_.token AS token_6,u0_.facebookId AS facebookId_7,u0_.userid AS userid_8,u0_.role AS role_9 FROM user u0_ WHERE(u0_.name LIKE'%User%'AND u0_.lastname LIKE'%Name%')或u0_.email LIKE'%[email protected]%'(length = 371)' –