How do I write the code to save into my database based on the value in my dropdownlist when the save button is clicked.The dropdownlist value can either be equal to ‘weekly format’ or ‘Topic format’
The attribute name for the dropdownlist is ‘course_format’
for example,
If dropdownlist = ‘weekly format’
Then…
else
…
I want to do this in the controller.
model
class Course extends \yii\db\ActiveRecord
{
const TYPE_WEEKLY= 'Weekly format';
const TYPE_TOPIC='Topic format';
public static function tableName()
{
return 'course';
}
public static function getCourseFormat()
{
return[
Yii::t('app', self::TYPE_TOPIC) => Yii::t('app', 'Topic format'),
Yii::t('app', self::TYPE_WEEKLY) => Yii::t('app', 'Weekly format'),
];
}
}
public function actionCreate()
{
$request = Yii::$app->request;
$model = new Course();
// if post data was submitted, try to load it into the new model
if ($request->isPost && $model->load($request->post())) {
// check if topic format
if($model->course_format == Course::TYPE_TOPIC){
// do your topic related stuff
}
// check if weekly format
if($model->course_format == Course::TYPE_WEEKLY){
// do your weekly related stuff
}
// don't forget to save your model
if($model->save()){
// redirect user to view
return $this->render('view', ['model' => $model->id]);
}
}
// will be executed by default
// when no post data is submitted or model->save() failed
return $this->render('create', [
'model' => $model,
]);
}
Don’t forget that you also have to change your actionUpdate…
EXAMPLE 2
Alternatively you could write a function directly inside your Course Model:
public function handleFormat()
{
// check if topic format
if($this->course_format == self::TYPE_TOPIC){
// do you topic related stuff
// ...
// return true to indicate success
return true;
}
// check if weekly format
if($this->course_format == self::TYPE_WEEKLY){
// do your weekly related stuff
// ...
// return true to indicate success
return true;
}
// default => handling the format did not work
return false;
}
Then you can do in your Controller Create and Update actions something like:
public function actionCreate()
{
$request = Yii::$app->request;
$model = new Course();
// if post data was submitted, try to load it into the new model
if ($request->isPost && $model->load($request->post())) {
// handle format and save model
if($model->handleFormat() && $model->save()){
// redirect user to view
return $this->render('view', ['model' => $model->id]);
}
}
// will be executed by default
// when no post data is submitted or model->save() failed
return $this->render('create', [
'model' => $model,
]);
}
Example 2 is usually better, because you do not have to write the Code twice in Create and Update Actions.
Also when you have to make changes to "handleFormat" it automatically applies to create & update (and everywhere lese you use it).
Thanks a lot, you solved the problem. Am very grateful.
Please just one more issue.
If course_format is weekly and course_format_no is 3, the start_date will be selected. If start_date is 18th November, 2016. then lecture_name will be something like this:
18 November - 24 November,
25 November - 1 December,
2 December - 8 December.
And lecture_code will be something like this:
1,
2,
3
Note that lecture_name is increased by one week.
lecture_name and lecture_code are columns in lectures table.
It should be saved in different fields and not as a big string.