public function actionBook($id) { $model = new Booking(); $bookingModelAttributes = ['group_num', 'agency_id']; $customFieldsAttributes = array_map(function ($item) { return 'field' . $item['id']; }, $this->findModel($id)->getFields()->all()); $dynamicModelAttributes = array_merge($bookingModelAttributes, $customFieldsAttributes); $dynamicModel = new DynamicModel($dynamicModelAttributes); $dynamicModel->addRule($dynamicModelAttributes, 'required'); if ($dynamicModel->load(Yii::$app->request->post()) && $dynamicModel->validate()) { $attributes = $dynamicModel->getAttributes($bookingModelAttributes); $model->link('tour', $this->findModel($id)); $model->agency_id = $attributes['agency_id']; $model->group_num = $attributes['group_num']; $model->time = time(); if ($model->validate()) { $fields = []; $keys = ['booking_id', 'tour_id', 'field_id', 'value']; foreach ($dynamicModel->getAttributes($customFieldsAttributes) as $key => $field) { if (in_array($key, $customFieldsAttributes)) { $fields[] = [$model->id, $id, str_replace('field', '', $key), $field]; } } if (!empty($fields)) { Yii::$app->db->createCommand()->batchInsert('booking_fields', $keys, $fields)->execute(); $model->save(); return $this->redirect('/booking'); } } else { var_dump($model); $model->delete(); } } return $this->render('book', ['model' => $dynamicModel, 'tourModel' => $this->findModel($id)]); }