D
D
dkrylov2017-03-17 17:05:47
Yii
dkrylov, 2017-03-17 17:05:47

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
b5fc78f5067f4b30b842dd4d638a3947.png
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
                )

        )

)

ProductController.php
$product = Product::find()->select(['id', 'name', 'price'])->with('options')->asArray()->where(['id' => $id])->one();

Product.php
public function getOptions()
{
        return $this->hasMany(OptionBundle::className(), ['product_id' => 'id']);
}

I broke my whole head how to implement a link with 3 tables - the value of these same options.
I perfectly understand that the task is quite trivial, but still, due to the fact that I just started to deal with the framework, I don’t know how to do it. Googling and reading the documentation, I just simply cannot figure out what's what. Please advise how this is resolved.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-03-17
@dkrylov

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 question

Ask a Question

731 491 924 answers to any question