php - How do i get rid of methods for view in model class -


in models create methods used in views this:

<?php $this->widget('cgridview', [ 'id' => 'sales-list', 'columns' => [     [         'header' => 'id',         'name' => 'id',         'type' => 'raw',         'value' => '$data->getviewid()'     ],     [         'header' => 'int id',         'name' => 'int_id',         'value' => '$data->getinternalid()',     ], 

in model have code

public function getinternalid() {     ... }  public function getviewid() {     ... } 

by creating methods model rapidly rises , don't this. want devide view methods other model methods, best practice this?

your models becoming large , unwieldy because following recommended way of "fat model , thin controller" design. have noticed models in medium-sized project on time become obese quickly!

first theory

to solve this, first has understood models not classes or objects. model layer.

the fundamental logic behind popularity , emergence of mvc design pattern philosophy of separation of concerns. fundamentally there 2 layers in mvc: presentation , model layers.

presentation layer breaks down further controllers, views, widgets, templates, layouts , on.

similarly model layer breaks domain objects, storage abstractions , services.

domain objects think of "models" generally, , active record can considered part of storage abstraction.

the tl;dr version there can multiple model files call same activerecord patterns

how slim model!

first separate logical functions different groups, example if user model contains set of functions authentication, set of functions analytic reporting on usage , set of functions calculating/formatting data views (gridview, widgets, listview etc) , set of functions have core actions needed rules , relations etc. split them in classes

class user extends cactiverecord {     public static function model($classname=__class__) {         return parent::model($classname);     }     // slimmed used class contains core functions      // rules, relations, attributelabels, , perhaps search functions generated gii      // + think needed other models  } 

your authentication class looks

class userauthentication extends user {       public static function model($classname=__class__) {           return parent::model($classname);      }      // functions related authentication } 

your ui/formatting class looks

class userui extends user {      public static function model($classname=__class__) {            return parent::model($classname);      }     // formatting , other view related functions  

}

your analatics/report class looks

class userreports extends user {     public static function model($classname=__class__) {           return parent::model($classname);      }      // functions used reports  } 

ideally should split these separate modules, along other models example report module contains report domain objects.

this makes programming in larger teams easier. tests, ui,controllers, layouts pretty can written independently of each other, , each module can communicate each other via services (the third model component) interfaces


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -