私は CakePHP 2.1 を使用してい
ます。次のモデルと関係があるとしましょう。
Posts
Edition
Posts
Editors
Posts
Tags
私は から推論しており、特定の に属するPostsController
すべてのものを見つけたいと思っています。それは関連しており、関連しています。 Posts
Edition
Editor.id=55
Tag.id=33
Posts
HABTM リレーションの条件に基づく検索が、検索の方向と検索の理由をEditor
.
$this->Editor->find('all',
array(
'conditions'=>array('Editor.id'=>55),
'contain'=>array('Post')
)
);
残念ながら、条件を付けたい HABTM リレーションが複数あるため、ここでは機能しません。
またcontain
、ブランチ ( ) を切り離すだけで、editors
親 ( ) のフィルタリングには役立たないため、使用は機能しませんposts
。
これを解決するにはどうすればよいですか?
アドホック結合に頼る必要があるのでしょうか? もしそうなら、誰かが私にどのようなアプローチを取るべきかを大まかに説明してもらえますか?
編集: 私が達成したいことを説明するためのいくつかの擬似コード:
$this->Post->find('all',
array('conditions'=>array(
'Edition.id'=>4,
// start pseudo code
'Post.Editor' => 'has at least one related editor with id=55',
'Post.Tag' => 'has at least one related tag with id=33',
)
)
);
よろしく、バート
ソリューションの編集: @RichardAtHome に従って、以下のソリューションを作成しました。(まだ)複数の結合条件を採用していませんが、それは必要ではないようです:
// Paginate over Cards that belong to current Subscription (belongsTo) AND the User is related to as an Editor (HABTM)
$this->paginate = array(
'fields' => array('id','title','workingtitle','attachment_count','subscription_id'),
'joins' => array(
// create Editor-join
array(
'table' => 'cards_users',
'alias' => 'Editor',
'type' => 'left',
'conditions' => array(
'Editor.card_id = Card.id'
)
),
array(
'table' => 'users',
'alias' => 'User',
'type' => 'left',
'conditions'=> array(
'User.id = Editor.user_id'
)
)
),
'conditions' => array(
'OR' => array(
// is Card in current subscription? (straightforward condition)
array('Card.subscription_id',$subscription_id),
// is current User assigned as Editor? (condition works with join above)
array('User.id' => $user['id'])
)
),
'limit' => 10
);