I'm making an online shop. I have two models: Product
and Category
. Product
can have one category, while category can have many products.
I've defined relationships in models
. I can access categories and products. But I want to get all products from specific category. I've tried many examples of relational queries with "lazy" and "eager" approach from official documentation, but no success. Can you please explain how to implement it?
Here's my code:
Category controller:
public function relations()
{
return array(
'products' => array(self::HAS_MANY, 'Product', 'category_id'),
);
}
Product controller:
public function relations()
{
return array(
'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
);
}
Thank you.
You can do
you can access fields like
This returns array and you can see content for
debugging purposes
byof you can use
foreach
loop to access eachindex