Doctrine是否支持IF語句?我得到以下錯誤:Doctrine中的簡單IF測試語句
Expected known function, got 'IF'
而與IF執行這個查詢:
$qb->select("c.id, IF(c.type_id LIKE 9, c.name, c.lastname) as name")
它正常工作,而重新用純SQL。任何解決方法?
Doctrine是否支持IF語句?我得到以下錯誤:Doctrine中的簡單IF測試語句
Expected known function, got 'IF'
而與IF執行這個查詢:
$qb->select("c.id, IF(c.type_id LIKE 9, c.name, c.lastname) as name")
它正常工作,而重新用純SQL。任何解決方法?
是在教義if
語句不支持您可以將其轉換爲case when
IF(c.type_id LIKE 9, c.name, c.lastname) as name
到
case when c.type_id = 9 then c.name else c.lastname end as name
UPDATE: 從註釋沒有concat
功能是允許在case-when
答案是非常允許的。下面是一個例子
mysql> select * from timesheets ;
+-----------+-------+----------+
| client_id | hours | category |
+-----------+-------+----------+
| 1 | 1.50 | onsite |
| 1 | 1.50 | onsite |
| 1 | 1.00 | remote |
| 2 | 1.50 | remote |
+-----------+-------+----------+
4 rows in set (0.00 sec)
mysql> select
case when category = 'onsite' then concat('ON',' ',hours) else hours
end as dd from timesheets ;
+---------+
| dd |
+---------+
| ON 1.50 |
| ON 1.50 |
| 1.00 |
| 1.50 |
+---------+
4 rows in set (0.00 sec)
這個回答可以幫助你http://stackoverflow.com/questions/25504632/substring-index-function-in-doctrine-orm/25508062#25508062。您可以創建函數類並將其註冊爲Doctrine以使用它。但是,如下所述,考慮你是否真的需要IF。 – Stepashka