将简单查询转换为cakephp查找控制器中的paginate查询

时间:2013-08-01 10:50:56

标签: cakephp cakephp-1.3

我有以下关联

  1. 用户拥有多个域名

  2. 域名有很多访问者

  3. 域名包含多个网页浏览量

  4. 以下是查询

        SELECT
        pv.name,pv.domain_id,pv.counter as page_visits, v.ip, v.counter as visits
    FROM (
        SELECT  `domains`.`name` ,  `pageviews`.`domain_id` , COUNT(  `pageviews`.`domain_id` ) as counter
        FROM  `pageviews` 
        LEFT JOIN  `domains` ON (  `domains`.`id` =  `pageviews`.`domain_id` 
        AND  `domains`.`user_id` =  '129' ) 
        WHERE  `pageviews`.`user_id` =  '129'
        GROUP BY  `pageviews`.`domain_id` 
        ORDER BY COUNT(  `pageviews`.`domain_id` ) DESC 
    ) as pv
    LEFT JOIN (
        SELECT  `domains`.`name` ,  `visitors`.`ip` ,  `visitors`.`domain_id` , COUNT(  `visitors`.`domain_id` ) as counter
        FROM  `visitors` 
        LEFT JOIN  `domains` ON (  `domains`.`id` =  `visitors`.`domain_id` 
        AND  `domains`.`user_id` =  '129' ) 
        WHERE  `visitors`.`user_id` =  '129'
        GROUP BY  `visitors`.`domain_id` 
        ORDER BY COUNT(  `visitors`.`domain_id` ) DESC
    ) as v
        ON pv.domain_id = v.domain_id
    

    我想转换为cakephp查找paginate的查询,因为cakephp不允许$ this-> paginate(“mysql query here”);

2 个答案:

答案 0 :(得分:1)

我建议您使用此工具。

http://dogmatic69.com/sql-to-cakephp-find-converter

这将为您创建此查询的自定义查找方法,允许您在模型中实现它。

答案 1 :(得分:0)

您只需执行

查询即可
$this->ModelName->query('your_sql_query');

这将为上面的复杂查询工作