开发者

OOP, MVC - Models and Objects

开发者 https://www.devze.com 2023-02-07 07:42 出处:网络
I\'m building a site with CodeIgniter, and my I have a model called Blog_model. Within Blog_model, there are methods to pull a list of posts for a specific topic, for example, getPopularPosts().

I'm building a site with CodeIgniter, and my I have a model called Blog_model.

Within Blog_model, there are methods to pull a list of posts for a specific topic, for example, getPopularPosts().

getPopularPosts() queries the posts table for a list of posts with a topic_id matching the one specified and sorts them by popularity. So, that's one query against the entire table of posts (let's assume this will be very large eventually) to find all posts with topic_id x.

Then, foreach result as an individual post id, it creates a new Post object. The Post class constructs a post by setting the field id.

To开发者_Go百科 return the contents of a Post, I assign $post->getPost();, which queries the posts table again to return the entire row for the given id.

This organization (AFAIK) follows a nice object oriented principle. But now, for every posts (again, let's assume thousands, millions, whatever...), I have to first query for a list of ids and then again to get each post's content. If I'm returning 30 posts, that means 31 separate queries.

Alternatively, I could break the object oriented pattern and pull * for each post in posts where topic_id = x. Then, I have one query that returns all 30 posts, but now I don't feel so object oriented.

What to do?


There is no reason to have that many queries. You're basically just looking for X number of posts that are from a particular topic ID... you should return this as one object and then iterate through the result in PHP because it is significantly faster to do it that way once you get to the point of having millions of rows

You should go about it more like this:

class blog_model extends CI_Model {

     function __construct(){
         parent::__construct();
     }

     function getPopularPosts($cat_id){
        /* Using method chaining here since you sound like you
           really want to utilize everything OO CI has to offer */
        $posts = $this->db->select('id, title, post_info')
                  ->where('topic_id', $topic_id)
                  ->get('posts');

        if($posts->num_rows() > 0){
             return $posts;
        }else{
             return FALSE;
        }
     }    
}

Then your controller would look like this:

class blog extends CI_Controller {

     function __construct() {
         parent::__construct();
     }

     function blog_posts($popular_post_id) {
         $this->load->model('blog_model');
         $posts = $this->blog_model->getPopularPosts($popular_post_id);

         if(!empty($posts){
             foreach($posts as $post){
                 echo $post->id;
                 echo $post->title;
                 echo $post->post_info;
             }
         }else{
             echo 'There are no posts';
         }

     }

 }

There is no benefit (and actually a big problem) with generating a ton of queries in the fashion that you currently have it set up, vs generating one object from the query and iterating through each of the rows in the controller and doing whatever you need with the data.

0

精彩评论

暂无评论...
验证码 换一张
取 消