2017-06-12 84 views
1

我有一個存儲庫功能,我想使用CASE WHEN然後ELSE,但教義不允許在ELSE NULL。當然,我GOOGLE了,但我找不到答案如何避免它。主義CASE WHEN然後是NULL

public function countAverageOdd($user) { 
    return $this->getEntityManager() 
        ->createQuery(' 
          SELECT 
           avg(
            CASE 
             WHEN p.homeAway = \'h\' AND p.moneyline IS NOT NULL THEN m.home 
             WHEN p.homeAway = \'d\' AND p.moneyline IS NOT NULL THEN m.draw 
             WHEN p.homeAway = \'e\' AND p.moneyline IS NOT NULL THEN m.away 
             WHEN p.homeAway = \'h\' AND p.spread IS NOT NULL THEN s.home 
             WHEN p.homeAway = \'e\' AND p.spread IS NOT NULL THEN s.away 
             WHEN p.homeAway = \'h\' AND p.total IS NOT NULL THEN t.over 
             WHEN p.homeAway = \'e\' AND p.total IS NOT NULL THEN t.under 
            ELSE NULL 
            END 
            ) 
          FROM 
           AppBundle:Predictions p 
           LEFT JOIN p.moneyline m 
           LEFT JOIN p.spread s 
           LEFT JOIN p.total t 
          WHERE p.user = :user ') 
        ->setParameter(":user", $user) 
        ->setMaxResults(1) 
        ->getSingleScalarResult(); 
} 

錯誤:[語法錯誤] 0行,列900 :錯誤:意外 'NULL'

https://github.com/doctrine/doctrine2/issues/3160

doctrinebot評論了2013年5月22日

問題以分辨率「不會修復」關閉

請給我建議。

回答

1

我設法通過傳遞空值作爲參數來繞過主義邏輯檢查。這是一個黑客攻擊,但它似乎是唯一有效的工具。

public function countAverageOdd($user) { 
    return $this->getEntityManager() 
        ->createQuery(' 
          SELECT 
           avg(
            CASE 
             WHEN p.homeAway = \'h\' AND p.moneyline IS NOT NULL THEN m.home 
             WHEN p.homeAway = \'d\' AND p.moneyline IS NOT NULL THEN m.draw 
             WHEN p.homeAway = \'e\' AND p.moneyline IS NOT NULL THEN m.away 
             WHEN p.homeAway = \'h\' AND p.spread IS NOT NULL THEN s.home 
             WHEN p.homeAway = \'e\' AND p.spread IS NOT NULL THEN s.away 
             WHEN p.homeAway = \'h\' AND p.total IS NOT NULL THEN t.over 
             WHEN p.homeAway = \'e\' AND p.total IS NOT NULL THEN t.under 
            ELSE :null 
            END 
            ) 
          FROM 
           AppBundle:Predictions p 
           LEFT JOIN p.moneyline m 
           LEFT JOIN p.spread s 
           LEFT JOIN p.total t 
          WHERE p.user = :user ') 
        ->setParameters(array(":user"=> $user,":null"=>NULL)) 
        ->setMaxResults(1) 
        ->getSingleScalarResult(); 
+0

我可以在下個星期一嘗試它,我會發布結果。 – NTsvetkov

+0

謝謝!它的工作原理! – NTsvetkov

1

結帳出myql case operator documentation。該ELSE是可選的,因此你可以將其刪除

public function countAverageOdd($user) { 
    return $this->getEntityManager() 
        ->createQuery(' 
          SELECT 
           avg(
            CASE 
             WHEN p.homeAway = \'h\' AND p.moneyline IS NOT NULL THEN m.home 
             WHEN p.homeAway = \'d\' AND p.moneyline IS NOT NULL THEN m.draw 
             WHEN p.homeAway = \'e\' AND p.moneyline IS NOT NULL THEN m.away 
             WHEN p.homeAway = \'h\' AND p.spread IS NOT NULL THEN s.home 
             WHEN p.homeAway = \'e\' AND p.spread IS NOT NULL THEN s.away 
             WHEN p.homeAway = \'h\' AND p.total IS NOT NULL THEN t.over 
             WHEN p.homeAway = \'e\' AND p.total IS NOT NULL THEN t.under 

            END 
            ) 
          FROM 
           AppBundle:Predictions p 
           LEFT JOIN p.moneyline m 
           LEFT JOIN p.spread s 
           LEFT JOIN p.total t 
          WHERE p.user = :user ') 
        ->setParameter(":user", $user) 
        ->setMaxResults(1) 
        ->getSingleScalarResult(); 
+0

不要工作... HTTP://s18.postimg.org/ilgycfsu1/screenshot_203.png – NTsvetkov

+0

也許是我太急於回答,我沒有檢查,如果這實際上是由教義DQL支持,但我會做。你可以從symfony分析器發佈相關*可運行查詢*嗎? – lordrhodos

+0

運行查詢之前它會轉儲錯誤。 :-( – NTsvetkov