I have a model that I use for two different but similar uses. The model is used for job opportunities and for students to post a brief description of themselves and qualifications. Rather than make two different tables and two different models, I just added a field to indicate that the posting was a student post (type = 0) or employer post (type = 1).
I create the two posts in two different controller functions
public function actionStudentPost()
{
$model=new Posting('student');
$model->type = 0;
$model->location ="I am willing to re-locate";
///
$model=new Posting('employer');
$model->type = 1;
$model->location ="Various";
So I will be creating a ‘Posting’ with scenario of ‘student or employer’ two of the field values are already defined when the application displays the form but I also want to change the labels on the form rather than call two different forms.
In my model I check the scenario and assign labels.
public function attributeLabels()
{
switch($this->scenario){
case 'student':
return array(
'id' => 'ID',
'display' => 'Display',
'type' => 'Type',
'creationDate' => 'Creation Date',
'updateDate' => 'Update Date',
'area' => 'My area of interest/expertise',
'title'=>'Name and Education',
'details' =>'Some information about me',
'location' =>'This is where I would like to live',
'email' => 'My Contact Email',
);
break;
default: // employers end up here
return array(
'id' => 'ID',
'display' => 'Display',
'type' => 'Type',
'creationDate' => 'Creation Date',
'updateDate' => 'Update Date',
'area' => 'Area of expertise required',
'title'=>'Breif description of opportunity',
'details' =>'Opportunity details',
'location' =>'Geographic location of opportunity',
'email' => 'Contact Email',
);
break;
}
}
This all works perfectly but is it a hack? is there a better way to use Yii when these are the desired results?
I have found a few other posts that ask similar questions but no answers were provided is the scenario ‘ON’ only used in the rules?
doodle