0

我有以下协会

  1. 用户有很多域

  2. 域有很多访问者

  3. 域有很多综合浏览量

以下是查询

    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 查找分页查询,因为 cakephp 不允许 $this->paginate("mysql query here");

4

2 回答 2

1

我建议试试这个工具。

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

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

于 2013-08-01T14:56:50.867 回答
0

您可以简单地执行查询

$this->ModelName->query('your_sql_query');

这将为您完成上述复杂查询的工作

于 2013-08-06T09:39:08.110 回答