我有桌子 1)旅游(身份证,头衔) 2)类别(id,title) 3)tours_categories(tour_id,category_id)
模特之旅:
public function relations ()
{
return array (
'Category' => array (self :: MANY_MANY,
'Categories',
'tours_categories (tour_id, category_id)'
),
);
}
模型类别:
public function relations ()
{
return array (
'Tours' => array (self :: MANY_MANY,
'Tours',
'tours_categories (category_id, tour_id)'
),
);
}
问题:
我想在数据库中搜索表tours_categories并选择所有的旅程=一个类别ID ...如何正确地执行
在控制器ToursController中,我想做类似这样的事情
$tour = Tours::model()->with ('category')->findAllByAttributes (array ('category.id' => $id));
但它当然不起作用。怎么做?
答案 0 :(得分:1)
$tours = Tours::model()->with(array(
'category'=>array(
'alias' => 'ct', //to avoid error ambiguous column category when implementing query
'condition'=>'ct.category_id = :cid',
'params'=>array(':cid'=>$id
)))
)->findAll(); // you could put more condition for findAll, it returns array of Tour-s after filtered.
顺便说一句,一件小事,模型Tour上关系MANY_MANY的名称category
不一致。它应改为categories
。更新后,您还应该更新上面的原因查询。