There is an array of $catID .
Here are its meanings:
array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } Trying to run it to save through foreach. Here is the code:
public function actionCreate() { $model = new Serial(); $category = Category::find()->where('tags<>1')->all(); $catSerial = new CatSerial(); if ($model->load(Yii::$app->request->post())) { $post=Yii::$app->request->post(); $catID=$post['CatSerial']['id_cat']; $model->save(); foreach($catID as $cat){ $catSerial->id_cat=$cat; $catSerial->id_serial=$model->id; $catSerial->isNewRecord = true; $catSerial->save(); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', [ 'model' => $model, 'category' => $category, 'catSerial' => $catSerial ]); } } The problem is that save() processed once the last why? And how to make work twice.
id_catnot autoincrement? can try$catSerial->id_cat = NULL? either do not write at all$catSerial->id_cat=$cat- Alexey Shimanskybatchinsertyiiframework.com/doc-2.0batchinsert... forming a pre-array ..... besides, you can write such a request with a lot ofvalues.... but also through DAO - Alexey Shimansky