I
I
Igor Fedyaev2021-02-21 13:03:18
Yii
Igor Fedyaev, 2021-02-21 13:03:18

Multidimensional linked array up to 3 levels?

Hello. Is it possible to connect other models to the model in conjunction by id?
The task is this: There is a Store where there are formed orders, orders have ordered goods, the goods have statuses.
Model:

class Orders extends \yii\db\ActiveRecord
{
    public static function tableName()
    {
        return 'orders';
    }

    public function getOrderProducts()
    {
        return $this->hasMany(OrderProducts::className(), ['order_id' => 'id']); // Выводит массив с товаром
    }

    public function getStatus()
    {
        return $this->hasOne(OrderStatuses::className(), ['id' => 'status_id']); // Выводит статус к order
    }

}


Controller:
public function actionMyOrders()
    {

        if (!Yii::$app->user->isGuest) {
            $orders = Orders::find()
->where(['user_id' => Yii::$app->user->identity['id']])
->with(['status', 'orderProducts', ])
                ->asArray()->all();
        } else {
            return Yii::$app->response->redirect(['/mart/login']);
        }
        return $this->render('myOrders', [
            'orders' => $orders
        ]);

    }


View(Debug):
Array
(
    [0] => Array
        (
            [id] => 174
            [invoice_order] => 2021-19-ALM-174
            [payment_method_id] => 
            [order_comments] => 
            [billing_first_name] => 
            [billing_last_name] => 
            [billing_phone] => 
            [billing_email] => 
            [terms_field] => 
            [terms] => 
            [ip] => 37.99.87.214
            [user_id] => 68
            [status_id] => 1
            [created_at] => 2021-02-19 20:33:21
            [updated_at] => 2021-02-19 20:33:21
            [status] => Array
                (
                    [id] => 1
                    [name_status] => В обработке
                    [status] => 1
                    [created_at] => 
                    [updated_at] => 
                )

            [orderProducts] => Array
                (
                    [0] => Array
                        (
                            [id] => 261
                            [order_id] => 174
                            [product_id] => 85
                            [product_name] => Название товара
                            [quantity] => 1
                            [price] => 4200
                            [total] => 4200
                            [status_id] => 1
                            [updated_at] => 2021-02-19 20:33:21
                            [created_at] => 2021-02-19 20:33:21
                        )

                    [1] => Array
                        (
                            [id] => 263
                            [order_id] => 174
                            [product_id] => 83
                            [product_name] => Название товара
                            [quantity] => 1
                            [price] => 600
                            [total] => 600
                            [status_id] => 1
                            [updated_at] => 2021-02-19 20:33:21
                            [created_at] => 2021-02-19 20:33:21
                        )

                )

        )
)

As you can see, there is a link order -> status. Here I can not add a status to the product.
It should be like this ...
60322f897ddc1496316102.png

Maybe someone will have the logic of a different approach to this implementation, please advise.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question