Cakephp在递归条件下的分页

时间:2011-07-03 07:54:10

标签: cakephp pagination

我在这个问题上生气,请有人帮助我:)。

我有这个型号:

订单hasMany - > Orderitem hasOne - >产品

产品有字段vendor_id。

我想对具有特定vendor_id的产品的订单进行分页。

我怎么能做到这一点?

我在orders_controller中的代码:

if(!empty($this->data['Order']['vendor_id'])) {
    $conditions['Product.vendor_id']=$this->data['Order']['vendor_id'];
}
if(!empty($this->data['Order']['startdate'])) {
    $conditions['Order.date >=']=$this->data['Order']['startdate'];
}
if(!empty($this->data['Order']['enddate'])) {
    $conditions['Order.date <=']=$this->data['Order']['enddate'];
}
$this->paginate=array('conditions'=>$conditions,'order'=>'Order.id ASC');

它在'where子句'错误中提供了未知列'Product.vendor_id'。 我用Containable尝试了几件但没有成功的事情:(

1 个答案:

答案 0 :(得分:5)

可能只有在设置vendor_id时才将recursive属性设置为2:

if(!empty($this->data['Order']['vendor_id'])) {
    $this->paginate['recursive'] = 2;
    $conditions['Product.vendor_id']=$this->data['Order']['vendor_id'];
}
if(!empty($this->data['Order']['startdate'])) {
    $conditions['Order.date >=']=$this->data['Order']['startdate'];
}
if(!empty($this->data['Order']['enddate'])) {
    $conditions['Order.date <=']=$this->data['Order']['enddate'];
}
$this->paginate['conditions'] = $conditions;
$this->paginate['order'] = 'Order.id ASC';

或者,如果您愿意,可以将递归设置为-1并手动构建连接。例如:

$this->paginate = array('fields'=>'Order.*',
                        'conditions'=>$conditions,
                        'joins'=>array( array(  'table' => 'orderitems',
                                        'alias' => 'Orderitem',
                                        'type' => 'INNER',
                                        'conditions' => array('Order.id = Orderitem.order_id')),
                                 array( 'table' => 'products',
                                        'alias' => 'Products',
                                        'type' => 'INNER',
                                        'conditions' => array('Product.id = Orderitem.product_id',
                                                              'Product.vendor_id'=>$this->data['Order']['vendor_id'])));

希望这会有所帮助,祝你好运