Answer the question
In order to leave comments, you need to log in
How to properly link 3 tables in yii2?
Good afternoon, dear programmers;)
I'm slowly understanding the yii2 framework, namely, I'm writing an online store. I want to achieve some flexibility, and therefore I use this approach:
1) Product - A table with products and general characteristics
2) Product_options - Product options (for example, name: color, value: black)
3) options_bundle - a link between products and a specific option
Using hasMany() produces an array like this:
Array
(
[id] => 1
[name] => Товар 1
[price] => 100.2
[options] => Array
(
[0] => Array
(
[id] => 1
[product_id] => 1
[option_id] => 1
)
)
)
$product = Product::find()->select(['id', 'name', 'price'])->with('options')->asArray()->where(['id' => $id])->one();
public function getOptions()
{
return $this->hasMany(OptionBundle::className(), ['product_id' => 'id']);
}
Answer the question
In order to leave comments, you need to log in
Good afternoon.
I'm sure this video will help you figure it out.
For now, try to get the color of the model through a link, something like this:
This is for a call to Product, through the options link you get additional parameters.
See also here , working with connected data.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question