R
R
RuslanTii2020-07-10 12:51:18
Yii
RuslanTii, 2020-07-10 12:51:18

Why are several products not being written to the database table?

Good day, I ran into such a problem:
There are 2 tables in the database, one for the details of the order, and the other for the goods of this order.
I place an order, data is written to the first table (for order details), and only 1 row is written to the second table, instead of 2, 3, etc. (for example, if there are 3 types of goods, 3 lines with goods should be recorded, but I only have one line).

$order = new Orders();
$items = new OrdersItems();

//Сохранение заказа ...
// ................


//Сохранение товаров
            if ($order->order_id){
                foreach ($_SESSION['cart'] as $item){
                    $items->order_id = $order->order_id;
                    $items->product = $item['name'];
                    $items->product_id = $item['product_id'];
                    $items->price = $item['price'];
                    $items->qty = $item['qty'];
                    $items->save();
                }
                $session =Yii::$app->session;
                $session->open();
                $session->remove('cart');
                $session->remove('cart.qty');
                $session->remove('cart.sum');
                return $this->redirect(['orders/view', 'id' => $order->order_id]);
            }else{
                debug('Error!');
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Larionov, 2020-07-11
@must_be_alone

Saved on last loop, easier to use $model->insert query

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question