动态表单控制器
public function actionCreate()
{
$model = new Courses();
$modelsCourseInstructors = [new CourseInstructors];
if ($model->load(Yii::$app->request->post())) {
$modelsCourseInstructors = Model::createMultiple(CourseInstructors::classname());
Model::loadMultiple($modelsCourseInstructors, Yii::$app->request->post());
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsCourseInstructors) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
foreach ($modelsCourseInstructors as $modelCourseInstructors) {
$modelCourseInstructors->course_instructor_course_id = $model->course_id;
if (!($flag = $modelCourseInstructors->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $model->course_id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
// return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
'modelsCourseInstructors' => (empty($modelsCourseInstructors)) ? [new CourseInstructors] : $modelsCourseInstructors
]);
}
}
我尝试了几种方法,但代码不仅仅是工作。 每项工作都是独立完成的,但将两者结合起来就是我所面临的问题。 我该怎么做才能使它发挥作用。第二个控制器如下所示。
复杂表单控制器
public function actionCreate()
{
$model = new Courses();
$request = Yii::$app->request;
if ($model->load(Yii::$app->request->post()) && Yii::$app->request->isAjax) {
//The course was created successfully, so we can use its data to make course structure
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ($model->load(Yii::$app->request->post())) {
$model->attributes = $_POST['Courses'];
$model->course_start_date = date('Y-m-d', strtotime($_POST['Courses']['course_start_date']));
$model->created_at = new \yii\db\Expression('NOW()');
$coursefile = UploadedFile::getInstance($model, 'coursefile');
if (!is_null($coursefile)) {
// if ($this->validate()) {
foreach ($this->coursefile as $file) {
// $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
$model->course_file_name = $coursefile->name;
$ext = end((explode(".", $coursefile->name)));
// generate a unique file name to prevent duplicate filenames
$model->course_file_path = Yii::$app->security->generateRandomString() . ".{$ext}";
// the path to save file, you can set an uploadPath
// in Yii::$app->params (as used in example below)
//$Photo = Yii::getAlias('@web').'/../data/ins_images/no-photo.png';
Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/../web/data/course_admin_files/course_files/';
// $model->course_file_path->saveAs(Yii::getAlias('@webroot').'/data/course_admin_files/course_files/'.$model->course_file_path = 'Course_'.date("d-m-Y_His").'.'.$model->course_file_path->extension);
//$model->course_file_path->saveAs(Yii::$app->basePath.'/web/data/course_admin_files/course_files/'.$model->course_file_path = 'Course_'.date("d-m-Y_His").'.'.$model->course_file_path);
$path = Yii::$app->params['uploadPath'] . $model->course_file_path;
$file->saveAs($path);
}//foreach
}
$model->save(false);
if ($model->save(false)) //The course was created successfully, so we can use its data to make course structure
{
// check if topic format
if ($model->course_format == Courses::TYPE_TOPIC) {
//loop through and make the course structure with topic#
// do your topic related stuff
for ($i = 1; $i <= $model->course_format_no; $i++) {
$structure = new CourseStructure();
$structure->course_id = $model->course_id;
$structure->structure_name = $model->course_format . $i;
$structure->structure_id = $i;
// fill in other course structure data here
$structure->save();
}
}
// check if weekly format
if ($model->course_format == Courses::TYPE_WEEKLY) {
}
return $this->redirect(['index']);
} else
return $this->render('create', ['model' => $model,]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
我想创建一个可以执行这两个功能的表单。 **如何将代码组合成一个单一控制器**