Answer the question
In order to leave comments, you need to log in
Why in yii2 in the controller in the field where the id is written through a loop, the save model gives an error that this is an array?
Here is such a control, for some reason this line $item->order_id = $model->id; gives an error when saving that this is an array, although if you wardump $item->order_id or $model->id it gives the usual int number
Here is the controller
public function actionOrder() {
$model = new Order();
$item = new OrderItem();
$cart = $this->service->getCart();
if ($model->load(Yii::$app->request->post())) {
$model->status = 'on';
if ($model->save()) {
$items = $this->cart->getItems();
foreach ($items as $product) {
$item->order_id = $model->id;
$item->item_id = $product->getProductId();
$item->quantity = $product->getQuantity();
$item->item_price = $product->getCost();
if(!$item->save()){
var_dump($item->errors);
die;
}
}
}
}
return $this->redirect(['index']);
}
Answer the question
In order to leave comments, you need to log in
Good afternoon.
Maybe that would be better?
foreach ($items as $product) {
$item = new OrderItem();
$item->order_id = $model->id;
$item->item_id = $product->getProductId();
$item->quantity = $product->getQuantity();
$item->item_price = $product->getCost();
if(!$item->save()){
var_dump($item->errors);
die;
}
}
To begin with, instead of
foreach ($items as $product) {
$item->order_id = $model->id;
foreach ($items as $product) {
$item = new OrderItem();
$item->order_id = $model->id;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question