Calling Model Method From Controller

Hii

I’m calling model method (info.php) in controller (siteController.php)…is working but i don’t know if i’m doing it from best/good way.

My Code:

controllers/SiteController.php




public function actionEdit(){

      $info= info::findOne(22);

      $newdate = date('d-M-Y', $info->Date);

      $info->Date = Info::changeDate($newdate);


      return $this->render('test', [

             'info'=>$info

      ]);

}



models/Info.php




public function changeDate($date){

    return "example";

}



Thank you.

Did you have a look at the example application that comes with Yii? Should give you a pretty good idea of how to approach things.

Your edit action always edits the same record, you’d want the identifier to come in as a parameter.

I know, it’s only one example to show…my doubt is in calling method nothing more.

Name the class file the same way as class itself, it will save you a lot of trouble on case-sensitive systems.

changeDate method is not declared as static so don’t call it like static method. Or declare it as static and do.

And if it’s not static why not change Date attribute directly in it instead of returning value?

Thanks for your reply. I did the changes that u said.

Everything works fine.