Hi,
I have 2 tables product table and category table. I want to get only categories which have products. I get the "Trying to get property of non-object" error rather PHP Notice. Particular I need to know whether I have defined relations correctly. I still did not get Relational Active Record clearly. Necessary diagrams and code snippets are given. I would be much appreciate if any one can help on this.
Product class is given below.
class Product extends CActiveRecord
{
public function relations()
{
return array(
'category' => array(self::BELONGS_TO, 'Category', 'category'),
);
}
Category class is given below.
class Category extends CActiveRecord
{
public function relations()
{
return array(
'products'=>array(self::HAS_MANY, 'Product', 'category_id'),
);
}
controller is given below
// Get the categories
$arrCategories = Category::model()->with('products')->findAllByAttributes(array('status'=>1), array('order'=>'sort_order ASC'));
$i = 0;
foreach($arrCategories as $category)
{
//echo $category->category_id;die;
$this->categories[$i]['category_id'] = $category->category_id;
$this->categories[$i]['category_name'] = $category->category_name;
$this->categories[$i]['description'] = $category->description;
$this->categories[$i]['category_image'] = $category->category_image;
$product = Product::model()->with('category')->find('category=:category_id', array(':category_id'=>$category->category_id));
$this->categories[$i]['product_id'] = $product->product_id; // This is where error got
$i++;
}