/** * Common function for actions create/update * @param Index $model * @param boolean $update * @return boolean */ private function indexCreateUpdate(&$model, $update = false) { $OK = true; if ($model->load(Yii::$app->request->post()) && $model->validate()) { if (isset($_POST['IndexQuotes']) && !empty($_POST['IndexQuotes'])) { $transaction = Yii::$app->db->beginTransaction(); // first delete all linked quotes in table indeces links, then recreate them if ($update) { $OK = $OK && Yii::$app->db->createCommand()->delete('indiceslinks', ['indid' => $model->indexid])->execute(); } $OK = $OK && $model->save(); foreach ($_POST['IndexQuotes'] as $qid) { $command = Yii::$app->db->createCommand()->insert('indiceslinks', ['indid' => $model->indexid, 'quoteid' => $qid]); $OK = $OK && $command->execute(); } if ($OK) { $transaction->commit(); return true; } else { $transaction->rollBack(); $model->addError('index', 'Some problems during transaction occured'); } } else { $model->addError('quotes', 'The quotes list must be at least 1 item long'); } } return false; }