简体   繁体   中英

CASE WHEN usage in Phalcon models manager doesn't give the right value

While looking at some code from my controller I saw that there is a weird bug on a query, basically on the case when statements :

Database content :

columns :

category : client or case category
type : title or content
mainpage 1 or 0 (true or false)

values :

0;0;1

corresponding to the normal output :

Case category;title;yes

current output :

Client;Content;No

PHQL :

SELECT content.id,
(CASE content WHEN content.category = 0 THEN 'Case category' WHEN content.category = 1 THEN 'Client' WHEN content.category = 2 THEN 'Case' WHEN content.category = 3 THEN 'Product' WHEN content.category = 4 THEN 'Product category' WHEN content.category = 5 THEN 'Team' WHEN content.category = 6 THEN 'Vacancy' WHEN content.category = 7 THEN 'Reference' WHEN content.category = 8 THEN 'Article' ELSE "Category not found" END) AS category 

FROM Apps\Source\Models\MainContent AS content 
GROUP BY content.id 

The fact is that all the 'case when' statements return the wrong value even tho (ex : content.category = 0 in the database, but it gets me 'Client event tho it's supposed to get it for a value of 1)

If I translate the statement into pure sql, just for the case, to test it, it works...

SELECT 
content.id,
(CASE WHEN content.category = 0 THEN 'Case category' WHEN content.category = 1 THEN 'Client' WHEN content.category = 2 THEN 'Case' WHEN content.category = 3 THEN 'Product' WHEN content.category = 4 THEN 'Product category' WHEN content.category = 5 THEN 'Team' WHEN content.category = 6 THEN 'Vacancy' WHEN content.category = 7 THEN 'Reference' WHEN content.category = 8 THEN 'Article' ELSE "Category not found" END) AS category 

FROM main_content AS content 
GROUP BY content.id 

output :

Case category;title;yes

Is this a phql oddity ?

There is a difference between both statements (raw sql is searched case, PHQL is simple): in PHQL try

SELECT content.id,
(CASE WHEN ...

With so many cases, wouldn't it be better to make a "Category" lookup table and join it with "main_content"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM