开发者

CakePHP & Subselects

I got app that need to be recoded in CakePHP.

I got following select with subselects:

        SELECT COUNT(*) AS item1,
            (SELECT COUNT(*) FROM portal_members) AS item3, 
            (SELECT COUNT(*) FROM portal_reviews) AS item3, 
            (SELECT COUNT(*) FROM portal_downloads) AS item4
        FROM portal_articles 
        WHERE 1 = 1

Anyone have any i开发者_开发问答dea how to create that query using CakePHP find($type, $params) ?


If that is your exact query, I'd recommend doing it in four passes.

$item1 = $this->PortalArticle->find('count');
$item2 = $this->PortalReview->find('count'); // etc..

It might take a wee bit longer to run, but your intentions are much clearer and the code would be much cleaner.


Please try this code

$this->loadModel('PortalArticle');
$db = $this->PortalArticle->getDataSource();
$a = $db->fetchAll(
    ' SELECT COUNT(*) AS item1,
    (SELECT COUNT(*) FROM portal_members) AS item3, 
    (SELECT COUNT(*) FROM portal_reviews) AS item3, 
    (SELECT COUNT(*) FROM portal_downloads) AS item4
FROM portal_articles 
WHERE 1 = 1'
);


The CakePHP cookbook contains some subquery examples here:

http://book.cakephp.org/view/74/Complex-Find-Conditions

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜