利用CakePHP的Containable行为,可以很好的进行关联查询.
首先在模型中要声明该行为:
var $actsAs = array('Containable');
如下的语句对Deal,DealDetail,Order进行关联,其Model之间的关系是Deal has many DealDetails, Order belongs to DealDetail.
$this->Deal->contain(array('DealDetail', 'DealDetail.Order'));
还可以设置条件和记录条数限制等,比如仅仅返回订单状态成功的记录,不超过1000条.
$this->Deal->contain(array('DealDetail', 'DealDetail.Order'=>array('conditions'=>array('Order.status'=>'success'),'limit'=>1000)));
regards,
iefreer