我想在控制器中加入两个表..
paginate模型也有很多条件,但我想在这里简化它。
$Table = $this->Table->query('
SELECT Table.id FROM Table
INNER JOIN TableA ON Table.id = TableA.table_id
INNER JOIN TableA.Table_id = TabelB.id
WHERE
IFNULL(Table.x,0) <> 0
GROUP BY Table.id
HAVING COUNT(*) > 1');
$this->paginate = array(
'paramType' => 'querystring'
,'joins' => $Table
)
);
我对Cake感到满意,更多来自SQL背景,但这可以在控制器中完成,还是应该在模型中进行?
答案 0 :(得分:0)
$UserId = $this->Auth->user(id);
$this->Rate->bindModel(array('belongsTo'=>array('Vehicle'=>array('className'=>'Vehicle','foreignKey'=>'vehicle_id'),),),false);
$this->paginate = array('conditions'=>array('Rate.user_id'=>$UserId),
'order' => array('Rate.created DESC'),
'limit' =>'2'
);
$rating = $this->paginate('Rate');
答案 1 :(得分:0)
正如我在“控制器分页与子查询”中回答的问题一样,这个问题的回答是相同的
$this->paginate = array(
'paramType' => 'querystring',
'joins' => array(
array(
'table' => '(SELECT Table.id
FROM Table
INNER JOIN TableA ON Table.id = TableA.Table_id
INNER JOIN TableB ON TableA.TableB_id = TableB.id
WHERE
1 = 1
GROUP BY Table.id
HAVING COUNT(*) = 1)'
'alias' => 'Table_x',
'type' => 'INNER',
'conditions' => array('Table_x.id = Table.id')
)
)
);
$Table = $this->paginate();