Php Yii: parameter passing to action vs load model in action -
i'm trying optimize code, , can't decide on use, , whichever best practice.
i have view view1.php, rendered action. view1 contains model $model passed on view action; i'm using $model again use in different action below:
view1.php:
$studydetails = $this->actionstudydetails($model);
and in studydetails action, i'm going use $model,
studycontroller.php:
public function actionstudydetails($model){ //do processing of model here , return object }
my question is, idea pass entire object that's been loaded, supposing model large? in terms of optimization , or best practice?
or should pass id or primary key $model->id? , load model after; making action this:
studycontroller.php:
public function actionstudydetails($id){ $model = $this->loadmodel($id); //do processing of model here , return object }
should pass entire object unto action or best reload model once inside action? thanks, hope explained well
i prefer loading single row database. it's optimization wouldn't worry until becomes issue.
you can store model in controller prevent running same query multiple times:
// store model not repeat query. private $model; protected function loadmodel( $id = null ) { if($this->model===null) { if($id!==null) $this->model=somemodel::model()->findbypk($id); } return $this->model; }
it's trick learned here.
Comments
Post a Comment