我正在尝试在2个表episodes
和filters
之间使用HABTM关联。关联表为episodes_filters
。我想找到所有匹配过滤器ID 1和2的剧集。我的理解是CakePHP默认情况下会执行AND条件,但我的努力导致返回的结果与剧集ID 1或2匹配,或者根本没有结果。< / p>
以下是我到目前为止使用代码登陆的地方:
$filterConditions = array();
foreach($filterIds as $filterId ) {
$filterConditions[] = array('EpisodesFilters.filter_id' => $filterId);
}
$episodeFilters = $this->Episode->find('all', array(
'conditions' => array(
'Episode.organization_id' => $this->Auth->user('organization_id'),
'Episode.status' => $this->request->query['episode_status']
),
'joins' => array(
array('table' => 'episodes_filters',
'alias' => 'EpisodesFilters',
'type' => 'INNER',
'conditions' => array(
$filterConditions,
'EpisodesFilters.episode_id = Episode.id'
)
)
),
));
我尝试将每个过滤器ID设置为JOIN它自己的状态,而不是像以下数组那样:&#39; EpisodesFilters.filter_id&#39; =&GT;数组(1,2),但这会导致使用过滤器1或2返回剧集,而不是两者。
有人可以帮我确定如何为HABTM查找设置这个条件吗?
答案 0 :(得分:0)
foreach($filterIds as $filterId ) {
$filterConditions['and']['EpisodesFilters.filter_id'][] = $filterId;
}
没有循环的OR
$filterConditions['and']['EpisodesFilters.filter_id'] = $filterIds;
答案 1 :(得分:0)
首先制作AND
条件数组,从$filterIds
var:
foreach( $filterIds as $filter_id ) {
$filterConditions['AND'][] = array('EpidodesFilters.filter_id' => $filter_id);
}
然后你的find命令中的条件项将是:
'conditions' => array(
'AND' => $filterConditions
)