我有一个存储库函数,我想使用CASE WHEN THEN THEN,但是doctrine不允许在ELSE中使用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列:错误:意外“空”
https://github.com/doctrine/doctrine2/issues/3160
doctrinebot于2013年5月22日发表评论
问题以“无法修复”
的分辨率结束请给我建议。
答案 0 :(得分:4)
我设法通过传入null值作为参数来绕过Doctrines逻辑检查。这是一个黑客,但它似乎是唯一有效的,。
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();
答案 1 :(得分: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();
答案 2 :(得分:0)
另一种方法是使用NULLIF
,即supported。
例如
CASE WHEN WHEN p.homeAway = \'h\' THEN 'xyz' ELSE NULLIF(1,1) END
从https://github.com/doctrine/orm/issues/5801#issuecomment-336132280那里得到了
谢谢!