开发者

How can I properly use IF statement in doctrine and SELECT AS?

开发者 https://www.devze.com 2023-02-05 18:21 出处:网络
$query = Doctrine_Query::create() ->select(\'IF(m.sender_id = ?, m.receiver_id, m.sender_id) AS uid\', $my_id)
$query = Doctrine_Query::create()
->select('IF(m.sender_id = ?, m.receiver_id, m.sender_id) AS uid', $my_id)
->from('Message m')
->where('m.receiver_id = ? OR m.sender_id = ?', array($my_id,$my_id))
->groupBy('uid')

I am unable to group by 'uid', as if AS uid is not working at all...

If I try to retrieve generated sql via ->getSqlQuery() and try it directly in Mysql, it works.

SELECT IF(s.sender_id = 1, s.receiver_i开发者_如何转开发d, s.sender_id) AS s__0 
FROM message s 
WHERE (s.receiver_id = 1 OR s.sender_id = 1) GROUP BY s__0


$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder();
$q = $qb
      ->select("c.id")
      ->addSelect("CASE WHEN (c.parent IS NULL) THEN c.name ELSE 'something' END")
      ->from("MyBundle:Category", "c")
      ->leftJoin("c.parent", "t");

echo $q->getQuery()->getSQL();
0

精彩评论

暂无评论...
验证码 换一张
取 消