2016-07-28 29 views
0

首先,對不起英語不好!SQL to QueryBuilder

我嘗試轉換這個SQL(它的操作):

SELECT DISTINCT U.id 
FROM User U 
INNER JOIN Detail DE on U.id = DE.id_user 
INNER JOIN matiere MA on U.id = MA.id_user 
WHERE DE.ville = $var1 
AND MA.matiere = $var2 

查詢生成器。 我試試這個:

$query = $repository->createQuerybuilder('U.id') 
    ->from('User', 'U') 
    ->innerJoin('Detail', 'DE', 'WITH', ' U.id = DE.id_user') 
    ->innerJoin('matiere', 'MA', 'WITH', 'U.id = MA.id_user') 
    ->where('DE.ville = :ville') 
    ->setParameter('ville', $ville) 
    ->andWhere('MA.matiere = :matiere') 
    ->setParameter('matiere', $matiere) 
    ->distinct(); 

但我有此錯誤: 「[語法錯誤] 0行,列49:錯誤:字符串應爲結束,得到了 '' 「

當我試試這個:

$query = $repository->createQueryBuilder() 
    ->select('U.id') 
    ->from('User', 'U') 
    ->innerJoin('Detail', 'DE', 'WITH', ' U.id = DE.id_user') 
    ->innerJoin('matiere', 'MA', 'WITH', 'U.id = MA.id_user') 
    ->where('DE.ville = :ville') 
    ->setParameter('ville', $ville) 
    ->andWhere('MA.matiere = :matiere') 
    ->setParameter('matiere', $matiere) 
    ->distinct(); 

我有這樣的錯誤:

Warning: Missing argument 1 for Doctrine\ORM\EntityRepository::createQueryBuilder(), 

我教義和symfony3工作。

感謝您的幫助。

回答

0

這就像一個語法錯誤,在查詢生成器中作爲自己的查詢實例進行開發的功能,嘗試進行調試並查看SQL查詢是如何構建的,可能是語法錯誤。

$qb = $repository->createQueryBuilder(); 
$query = $qb->getQuery(); 
$debug = $query->debug(); 

具有串之間的空間[「U.id = DE.id_user」)],除去這些空間來嘗試以圍繞可能的錯誤;

或者你可以嘗試做這種方式,也許可以工作:

$query = $repository->createQueryBuilder(); <!--separte constructor this line--> 

    $query->select('U.id') 
    ->from('User', 'U') 
    ->innerJoin('Detail', 'DE', 'WITH', 'U.id = DE.id_user') <!--align this line--> 
    ->innerJoin('matiere', 'MA', 'WITH', 'U.id = MA.id_user') 
    ->where('DE.ville', ':ville') <!--separate this line--> 
    ->setParameter('ville', $ville) 
    ->andWhere('MA.matiere', ':matiere') <!--separate this line--> 
    ->setParameter('matiere', $matiere) 
    ->distinct() 
    ->getQuery(); <!--add this line--> 

    $resp = $query->getResult(); <!--get the answer this line--> 
+0

我試試你的解決方案,該錯誤是一樣的(警告:缺少參數1教義\ ORM \ EntityRepository :: createQueryBuilder()) 我已經嘗試刪除‘選擇(U.id’)和把'createQuerybuilder('U.id');' ('[Syntax Error] line 0,col 49:Error:Expected end of string,got'。'') 我不明白問題出在哪裏... – jomalix

+0

你做過調試嗎?知道變量中的SQL查詢如何, 似乎是在執行 SQL查詢時提示語法錯誤,錯誤[預期的字符串結束,得到。''],似乎是詞法分析器 它正在等待一個字符關閉字符串並正確執行它,或者有一個字符點[。 ] 亂序在SQL字符串中,中斷正常運行並生成錯誤 –

0

在這一行:

$query = $repository->createQuerybuilder('U.id') 

你試圖傳遞一個對象和方法調用,而不是隻是一個單一的參數。那爲什麼關於點的錯誤。

使用這個QueryBuilder的,而不是之前拿到ID:

$id = U.id 

$id = $U->getId(); 

然後傳遞參數:

$query = $repository->createQuerybuilder($id) 
... 

這樣你就不會得到錯誤:「[Syntax Error] line 0,col 49:錯誤:字符串的預期結束,得到'。' 「