codeigniter - Why protect tables and fields on top of every model? -
i have seen codeigniter application coder protected table , fields @ tp of every model. why this? , maybe why protect primary key separate other fields?
class students_model extends my_model{ protected $table = 'students'; protected $primary_key = 'id'; protected $columns = array( 'student_code' => array('code', 'trim|required'), 'student_name' => array('name', 'trim|required'), 'country' => array('country', 'trim|required'), ); }
this question understanding best practices in mvc (not codeigniter). there few different things going on here.
1) "protected" prolly done out of habit. protected php class keyword, , has nothing ci or models. can read here: http://jp2.php.net/protected; if after reading don't understand why used in case, it's because there isn't reason use in case.
2) key understanding rest "extends my_model". my_model base model object normal crud functions , utilities written; inherit it. however, our my_model have function get_all() say:
$this->db->from($this->table)->get();
for example. so, in our students model set $this->table = "students", above code translates to:
$this->db->from('students')->get();
but other models can pass different table name. able make simple table specific models , share more complex logic via my_model
you can see going #1, unlikely we'll ever inherit students_model, protected isn't harming anything, isn't terribly necessary
3) $columns in case validation rules; whichever my_model being used here adding validation functions my_model instead of putting on controllers
Comments
Post a Comment