Zxaos
Zxaos

Reputation: 8121

CakePHP select from subquery (SELECT foo from SELECT(...))

I have a CakePHP query that uses subqueries. While I've found documentation that shows me how to use subqueries in Cake for conditions, I haven't been able to find a way to use the subquery as the table.

In other words, I'm trying to express something like this:

SELECT `Status`.`name`,
COUNT(*) as total_count,
COUNT(NULLIF(over_one_year, 0)) as over,
COUNT(NULLIF(over_one_year, 1)) as under
FROM (
    SELECT ((YEAR('##some date##') - YEAR(COALESCE(start_date, '1900-01-01'))) -
    (RIGHT(DATE('##some date##'), 5) < RIGHT(COALESCE(start_date, '1900-01-01'), 5)) 
     >= 1) as over_one_year,
     status_id FROM `users` WHERE `user_id` IN (##some list of ids##)) as User

LEFT JOIN `statuses` AS `Status` ON (`User`.`status_id` = `Status`.`id`)

GROUP BY id;

in cake.

Is there any hope?

Upvotes: 1

Views: 1160

Answers (1)

sipiatti
sipiatti

Reputation: 914

I think this is beyond cakephp's ORM capabilities, or it is not worth to spend the time to find out how it could work with it. You can even use the $this->Model->query() method (http://book.cakephp.org/view/1027/query ) or at least it is still php, just write your own classes or functions to make special queries.

Upvotes: 1

Related Questions