控制器与子查询​​分页

时间:2016-06-07 13:55:26

标签: cakephp cakephp-2.3

我想在我的控制器中使用这个MySQL查询而不使用query()。

SELECT * FROM T1 INNER JOIN
(SELECT T1.id
FROM T1 
INNER JOIN T2 ON T1.id = T2.T1_id
INNER JOIN T3 ON T2.T3_id = T3.id
WHERE 
1 = 1
GROUP BY T1.id
HAVING COUNT(*) = 1) AS T1_x ON T1.id = T1_x.id

无法弄清楚我的控制器中的代码是什么。

1 个答案:

答案 0 :(得分:1)

以下是在CakePHP中加入子查询的一种方法

我的模型名称是T1

    $this->paginate = array(
    'paramType' => 'querystring',
    'joins' => array(
        array(
        'table' => '(SELECT T1.id
                    FROM T1 
                    INNER JOIN T2 ON T1.id = T2.T1_id
                    INNER JOIN T3 ON T2.T3_id = T3.id
                    WHERE 
                    1 = 1
                    GROUP BY T1.id
                    HAVING COUNT(*) = 1)'
            'alias' => 'T1_x',
            'type' => 'INNER',
            'conditions' => array('T1_x.id = T1.id')
            )
        )
);


$T1 = $this->paginate();