public function saveMultiple($isUpdate = null) { $authors_ids = $this->getAttributes()['a_id']; $book_id = $this->getAttributes()['b_id']; if ($isUpdate) { self::deleteAll(['b_id' => $book_id]); } foreach ($authors_ids as $item) { $model = new BookAuthorRel(); $model->b_id = $book_id; $model->a_id = $item; $model->save(); } }
/** * Updates an existing Books model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $model_b_a_rel = BookAuthorRel::findOne(['b_id' => $id]); $authorsArr = Authors::authorArr(); if ($model->load(Yii::$app->request->post()) && $model_b_a_rel->load(Yii::$app->request->post())) { $model->save(); $model_b_a_rel->saveMultiple(1); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model, 'model_b_a_rel' => $model_b_a_rel, 'authorsArr' => $authorsArr]); } }
/** * @return \yii\db\ActiveQuery */ public function getBookAuthorRels() { return $this->hasMany(BookAuthorRel::className(), ['a_id' => 'id']); }