Yii beginner here. I am facing some problem with how to query the database with the scenario I have. Here is what my two tables look like. The reason category is maintained in a separate table is because a product can belong to multiple categories.
Product Table
id
product_name
product_desc
product_color
product_price
Category Table
id
product_category
product_id
(A product can belong to multiple category.)
Now, let’s say I want to find the products of category ‘xyz’ with color ‘blue’. I am not sure how do I query both the tables using two different models (or not) to achieve this. Any help?
please carefully read what i post here, suppose i have two table: category and content where primry key table is category and relational table is content. now i want to assign a content to one or several categories (that is exactly you want i hope), so for this to happen,
in your content model add relation with this:
public function relations()
{
return array(
'cate' => array(self::BELONGS_TO, 'Category', 'cate_id'),
);
}
and in your category model add relation with this:
public function relations()
{
return array(
'contents' => array(self::HAS_MANY, 'Content', 'cate_id'),
);
}
and in your content model’s view/_form.php , add this: