First create MY_Model.php in application/core folder. Then create MY_Model class in it and the filename must be same as class name.
*you can also change MY_ prefix to your custom prefix.
Now you should extend MY_model instead of CI_Model in your Model class.
The first thing that comes in our mind is why we need this class when codeigniter already gives us bunch of DB functions in CI_Model class. The only thing that forced me to create this, I do not want code same lines that I use many time in my software. For example I created this method to check if data exist in same pattern based on given parameter:
or this example where I am fetching user's avatar from DB:
I just need to pass 2 - 3 parameters to them and they return me desirable data every time.
<?php
class MY_Model extends CI_Model {
public function __construct(){
parent::__construct();
$this->load->database();
}
}
class MY_Model extends CI_Model {
public function __construct(){
parent::__construct();
$this->load->database();
}
}
*you can also change MY_ prefix to your custom prefix.
Now you should extend MY_model instead of CI_Model in your Model class.
The first thing that comes in our mind is why we need this class when codeigniter already gives us bunch of DB functions in CI_Model class. The only thing that forced me to create this, I do not want code same lines that I use many time in my software. For example I created this method to check if data exist in same pattern based on given parameter:
<?php
class MY_Model extends CI_Model {
public function __construct(){
parent::__construct();
$this->load->database();
}
public function CustomCheckData($table, $where){
$query = $this->db->get_where($table, $where);
return ($query->num_rows())?true:false;
}
}
class MY_Model extends CI_Model {
public function __construct(){
parent::__construct();
$this->load->database();
}
public function CustomCheckData($table, $where){
$query = $this->db->get_where($table, $where);
return ($query->num_rows())?true:false;
}
}
or this example where I am fetching user's avatar from DB:
<?php
public function CustomUserAvatar($uid){
$this->db->select('avatar');
$query = $this->db->get_where('my_user_table', array('userid'=>$uid));
$db_data = $query->result_array();
return $fetch[0]['avatar'];
}
}
public function CustomUserAvatar($uid){
$this->db->select('avatar');
$query = $this->db->get_where('my_user_table', array('userid'=>$uid));
$db_data = $query->result_array();
return $fetch[0]['avatar'];
}
}
I just need to pass 2 - 3 parameters to them and they return me desirable data every time.
0 comments:
Post a Comment