2013-08-01 41 views
0

我想基於從請求中傳入的多個值對一個表執行簡單查詢,並且缺少對於更有經驗的人員來說顯而易見的事情。Doctrine QueryBulider語法錯誤

這不起作用:

public function showAction(Request $request) 
{ 
    if ($request->getMethod() == 'GET') { 
     $id = $request->get('locationid'); 
     $kfType = $request->get('type'); 
     $em = $this->getDoctrine() 
        ->getManager(); 

     $data = $em->createQueryBuilder() 
        ->select('d') 
        ->from('DashDataBundle:Data', 'd') 
        ->where('d.locationid = :locationid' AND 'd.kfType = :kfType') 
        ->setParameters(array('locationid'=> $id,'kfType'=> $kfType)) 
        ->setMaxResults(100) 
        ->getQuery() 
        ->getResult(); 
    } 

錯誤是:
警告:get_class()預計參數1爲對象,布爾/應用程序/ MAMP/htdocs中/路徑/供應商/教義/ ORM給定/lib/Doctrine/ORM/Query/Expr/Base.php線89

但是,這並不工作在只有一個參數:

public function showAction(Request $request) 
{ 
    if ($request->getMethod() == 'GET') { 
     $id = $request->get('locationid'); 
     $kfType = $request->get('type'); 
     $em = $this->getDoctrine() 
        ->getManager(); 

     $data = $em->createQueryBuilder() 
        ->select('d') 
        ->from('DashDataBundle:Data', 'd') 
        ->where('d.locationid = :locationid') 
        ->setParameter('locationid', $id) 
        ->setMaxResults(100) 
        ->getQuery() 
        ->getResult(); 
    } 

什麼我沒有到u nderstand?

回答

0

您將兩個字符串與AND組合起來,這就是爲什麼它會抱怨獲取布爾值。刪除多餘的引號,使AND成爲where函數中單個字符串參數的一部分。

+0

它必須是' - > where('d.locationid =:locationid AND d.kfType =:kfType')' –

+0

啊,謝謝!它現在有效。 – manisha