假如一个Deal包含多个语言版本的DealDetails
现在想在分页中获取
1)仅仅包含指定语言DealDetail的Deal列表
2)而且对于每个Deal仅仅返回指定语言的DealDetail
对于1如果只是使用如下的语句:
$this->paginate['Deal']['conditions']['DealDetail.lang_id'] = $this->data['lang_id'];
将会出现Unknown Column DealDetail.lang_id错误,即使你已经在Model中定义了Deal hasMany DealDetails, DealDetail belongsTo Deal
解决方法是使用'joins'参数
对于2,使用'containable'行为。
示范代码如下:
'Deal' => array(
'limit' => self::PAGE_LIMIT,
'order' => array(
'Deal.created' => 'DESC',
'Deal.id' => 'DESC',
),
'joins' => array(
array('table' => 't_deal_details',
'alias' => 'DealDetail',
'type' => 'LEFT',
'conditions' => array(
'DealDetail.deal_id = Deal.id',
)
)
),
'conditions' => array(
'DealDetail.lang_id' => 2,
),
'contain' => array(
'DealDetail' => array(
'conditions' => array(
'DealDetail.lang_id' => 2,
)
),
)
更多细节阅读
http://book.cakephp.org/view/1039/Associations-Linking-Models-Together
regards,
iefreer