How to use models in CodeIgniter?
I'm getting into CodeIgniter and trying to figure out the good architecture for my models. What kind of models would you create for the following simple example:
- list page of blog entries: shows part of the entry data, number of comments
- blog entry page: shows all the entry data, comment list (with part of the comment data)
- comment page: shows all the comment data
I'm trying to get this right so that it's simple and effective. I don't want to load too much information (from the db) on the pages where I don't need them.
E.g. should the same entry model han开发者_StackOverflow中文版dle both multiple entries as well as a single entry? And how should the comments be loaded? I only need the number of comments on the multiple entries (list) page but some of the comment data on the single entry page. How would you handle this?
Yes you're article_model model should handle all related queries and logic for any entry/article , however for comments related i would make a different model , let's call it comments_model .
Then you'll have a look at what data need to get displayed and build methods to retrive/handle that data in you're models .
Eg. For a blog entry page you'll need get_entry method in article_model , and get_article_comments in comments model , then for list page of blog you would need get_entryes and get_article_number_of_comments in comment model ....
Edit
For a page that list 20 blog entryes you'll have to make 1 query to list all blog entryes by calling article_model->get_entryes , and for displaing a 1 entry only you can call get_single_entry ( or get_entryes where you limit the result , or get_entry_by_uri , or get_entry_by_id ... ) . Then to get the number of comments for each entry you could make a get_multiple_nr_comment in the comment model where you pass entry ids and you're query would whave smth like "where comments.entry_id IN ( 1, 34, 55 ... )" there are a number of ways you can count multiple coments number in one query .
So you'll end up having 2 queries per page .
You could create MY_Model, something like this:
class MY_Model extends CI_Model {
var $table = '';//database table
function __construct(){
parent::__construct();
}
public function get( $id = NULL ) {
$query = $this->db->get_where($this->table, array('id' => (int)$id), 1, 0);
if ($query->num_rows() === 1) {
return = $query->result();
}
return NULL;
}
function get_one($id=NUNLL){}
function update($id=NULL, $data=array()){}
function delete($id=NULL){}
etc...
}
And then in your models per table you can extend MY_Model, something like this:
class My_Table_model extends MY_Model {
function __construct(){
parent::__construct();
$this->table='my_table';
}
}
You can then use methods from My_Model in your models. In your controller you can have something like this:
class Table_Controller extends Front_Controller {
function __construct(){
parent::__construct();
$this->load->model('my_table_model');
}
function comments(){
$this->my_table_model->get($get_id_somehow);
}
}
If You want to override something you can do it like this in your models that extends My_Model:
function my_method($id=NULL, $data=array()){
return parent::my_method($id, $data);
}
精彩评论