Hello,
This is my goal: Format the value of an Active Record model attribute differently per scenario;/action.
more specifically, Append the Currency Symbol OR another Custom Value of mine at the Attribute
only on the Index and View actions.
Using afterFind, I can alter the attribute’s value as I please, so this part is solved.
The problem is how to differentiate the formatting according to the action or scenario.
As we know, default scenarios are only update/insert for Index/View/Update
so I cannot use only this information to know when the user is Updating or Viewing the attribute values…
I tried in the controller to set the $model->setScenario(‘updateAction’);
in order to know when the update action is taking place and format accordingly.
Problem is, the scenario defaults to ‘update’.
In both case demonstrated below, the $this->scenario in the afterFind() equals ‘update’:
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$model->setScenario('updateAction');
if(isset($_POST['MyModel']))
{
$model->attributes=$_POST['MyModel'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
as well as when using this:
public function loadModel($id)
{
$model=MyModel();
$model->setScenario('updateAction');
$model=$model->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
Even though the Scenario has been set on the model before the findByPk is called,
the scenario value defaults to ‘update’ from my desired ‘updateAction’ when accessed inside afterFind().
Any ideas how to fix this, or perhaps, implement it properly?