/** * Удаление значения мультиполя * @return mixed */ public function actionDeletefield() { $field_id = Yii::$app->request->get('id'); /** @var \common\models\Field $field */ $field = Field::findOne($field_id); if ($field && isset($field->option)) { //необходимое кол-во значений мультиполя $count_require = $field->option->require; if ($count_require) { $all_fields = Field::find()->where(['option_id' => $field->option_id, 'document_id' => $field->document_id])->all(); if (count($all_fields) <= $count_require) { return $this->redirect(['update', 'id' => $field->document_id]); } } $field->deletePhoto(); $document_id = $field->document_id; $field->delete(); Yii::$app->getSession()->setFlash('field-delete-success'); return $this->redirect(['update', 'id' => $document_id]); } throw new NotFoundHttpException('Страница не найдена.'); }
/** * * Сохранение значения(-ий) дополнительного поля * @param $option_id - Тип дополнительного поля * @param $option - Дополнительное поле (со структурой) * @param string $type (value|file) * @param $z * @return bool */ protected function saveField($option_id, $option, $z, $type = 'value') { $x = 1; foreach ($option[$type] as $field_id => $value) { /** @var \common\models\Field $field[$z][$x] */ if (substr_count($field_id, 'new')) { $field[$z][$x] = new Field(); } else { $field[$z][$x] = Field::findOne($field_id); } if ($field[$z][$x]) { $field[$z][$x]->option_id = $option_id; $field[$z][$x]->document_id = $this->id; $field[$z][$x]->{$type} = $value; $field[$z][$x]->position = isset($option['position'][$field_id]) ? $option['position'][$field_id] : null; if ($field[$z][$x]->{$type}) { $field[$z][$x]->save(); if ($type == 'file') { $field[$z][$x]->savePhoto(); } } else { if (!$field[$z][$x]->isNewRecord && $type == 'value') { $field[$z][$x]->delete(); } } } $x++; } return true; }
/** * Finds the Field model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Field the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Field::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }