cakephp hasmany conditions
modelでhasmany conditions設定
結構悩んで、調べながらなんとか出来たのでメモ。
class Shop extends AppModel
{
var $hasMany = array(
'Girl' => array(
'className' => 'Girl',
'foreignKey' => 'shop_id',
'conditions' => array('Girl.mobile' => 'true'),
),
);
}
これだと必ずShop modelのデータが
‘conditions’ => array(‘Girl.mobile’ => ‘true’)
の条件になる。
Controller内で個別にhasmany conditions
さっきのmodelから’conditions’ => array(‘Girl.mobile’ => ‘true’),を削除して、
function test(){
$con = array('Girl.mobile' => 'true');//条件配列で
$this->Shop->hasMany['Girl']['conditions']=$con;
}
$post = $this->Post->find('all',$con);
これでやればよし!
今日のまとめ
hasmany conditionsについては応用が利くようにController内で個別にhasmany conditionsってやったほうがいいかも。仮にユーザーが自由に検索する動的なものでも対応できます