2012-04-14 23 views
4

我有这样的型号:从CodeIgniter(或任何MVC平台)中的数据库中提取数据?

public function index_loop() { 
     $post_user = $this->db->query("SELECT posts.post_title, posts.post_content, posts.post_date, users.username FROM posts LEFT JOIN users ON posts.user_id = users.user_id ORDER BY posts.post_ID DESC"); 
     //$categories = $this->db->query("SELECT categories.cat_name, categories.cat_id FROM 
     //$comments = $this->db->query("SELECT COUNT(comment_id) FROM comments WHERE 
     return $post_user->result_array(); 
    } 

我需要的是要为每个帖子和评论类(虽然我想,如果我弄清楚路过不是注释类别是相同的方式)

中查看文件:

<?php foreach($posts as $zz) { ?> 
     <div class="article"> 
      <h2><?php echo $zz['post_title']; ?></h2> 
      <p>Posted by <a href=""><?php echo $zz['username']; ?></a> | Filed under <a href="#">templates</a>, <a href=>internet</a></p> 
      <p><?php echo $zz['post_content']; ?></p> 
      <p><a href=>Read more</a> | <a href=>Comments (5)</a> | <?php echo $zz['post_date']; ?></p> 
     </div> <?php } ?> 

所以,如果我想在每个博客圈类别我需要博客ID,我怎么得到它,如果我做的型号所有查询? 同样的评论

是不是很好,我做了一个大型复杂的数据库查询(这是很难但我能做到)或者我可以做2或3个单独的较小的查询?

+0

我有单独的表,其中categories_id和posts_id不同,所以只有relation_id,cat_id,post_id关系表 – zarkoz 2012-04-14 12:21:32

回答

2

Codeigniter允许您将数据库结果作为对象(例如模型对象)返回,这使得数据更易于使用。您可以将初始查询发送到Posts表,将posts.id字段包含在结果集中,并将Post_model类的名称传递给$db->query->result()函数,以告知代码单元您希望将结果作为实例返回Post_model类。

然后,您可以通过post_id定义对Post_modelGetCategoriespost_idGetComments方法,然后调用这些方法来填充你的$categories$comments收藏从您的查询返回的每一Post_model

下面是一个例子,我希望它能帮助:

<?php foreach($posts as $zz) { ?> 
     <div class="article"> 
      <h2><?php echo $zz->post_title; ?></h2> 
      <p>Posted by <a href=""><?php echo $zz->username; ?></a> | 

       Filed under 
       <?php foreach($zz->categories as $category) { 
         echo '<a href="#">{$category->name}</a>, '; 
        } 
       ?> 
      </p> 
      <p><?php echo $zz->post_content; ?></p> 
      <p><a href=>Read more</a> | <a href=>Comments (5)</a> | <?php echo $zz->post_date; ?></p> 
     </div> <?php } ?> 

至于:

public class Post_model extends CI_Model 
    { 

     // All the properties in the Posts table, as well as a couple variables to hold the categories and comments for this Post: 
     public $id; 
     public $post_title; 
     public $post_content; 
     public $post_date; 
     public $username;   
     public $categories; 
     public $comments; 

     public function index_loop() 
     { 
       return $this->GetAllPosts(); 
     } 

     // function to get all posts from the database, including comments and categories. 
     // returns an array of Post_model objects 
     public function GetAllPosts() 
     { 
       // define an empty array to hold the results of you query. 
       $all_posts = array(); 

       // define your sql query. NOTE the POSTS.ID field has been added to the field list 
       $sql = "SELECT posts.id, 
           posts.post_title, 
           posts.post_content, 
           posts.post_date, 
           users.username 
         FROM posts LEFT JOIN users ON posts.user_id = users.user_id 
         ORDER BY posts.post_id DESC"; 

       // issue the query 
       $query = $this->db->query($sql); 

       // loop through the query results, passing a string to result() which represents a class to instantiate 
       //for each result object (note: this class must be loaded) 

       foreach($query->result("Post_model") as $post) 
       { 
         $post->categories = $this->GetPostCategories($post->id); 
         $post->comments = $this->GetPostComments($post->id); 
         $all_posts[] = $post; 
       } 

       return $all_posts; 
     } 

     // function to return categories for a given post_id. 
     // returns an array of Category_model objects. 
     public function GetPostCategories($post_id) 
     { 
       $sql = "SELECT category.id, ... WHERE post_id = ?"; 
       $query = $this->db->query($sql, array($post_id)); 
       $categories = array(); 
       foreach($query->result("Category_model") as $category) 
       { 
         $categories[] = $category; 
       } 
       return $categories; 
     } 

     // function to return comments for a given post_id. 
     //returns an array of Comment_model objects 
     public function GetPostComments($post_id) 
     { 
       $sql = "SELECT comment.id, ... WHERE post_id = ?"; 
       $query = $this->db->query($sql, array($post_id)); 
       $comments = array(); 
       foreach($query->result("Comment_model") as $comment) 
       { 
         $comments[] = $comment; 
       } 
       return $comments; 
     }  
} 

然后,在你看来,你可以为Post_model对象,而不是作为result_arrays访问$帖子阵列效率问题,它将取决于很多因素(数据库与Web服务器位于同一台计算机上,有多少帖子等等)。单个大型查询通常会比几个小型查询执行得更快,但需要通过分析来真正确定性能增益是否值得寻求。我总是倾向于尝试编写可读/可理解的代码,而不是以增加复杂性为代价进行优化。

+0

非常感谢您理解我的问题,这是我需要的,但我不知道它是这复杂但我认为我可以处理它只是我需要更多的努力:)我会回到你的只是一个小问题,我希望你不介意 – zarkoz 2012-04-16 12:05:58

+0

你通过“Post_model”结果$查询 - >导致foreach循环,在文档中它说“代表一个类来实例化每个结果对象(注意:这个类必须被加载)”就像你放入注释,那么$ query-> result(“Post_model”)会返回什么,如果它引用了当前类Post_model,它是否访问此类中的所有方法? – zarkoz 2012-04-16 12:11:24

+0

以及$ query-> result(“Category_model”)和Comments_model,我需要将它们放在单独的文件中? – zarkoz 2012-04-16 12:11:44

0

你可以做1件事。

作出不同的分类表和评论表。

category_id将与post_table中的category_id列相链接。

在评论表中,创建将与post_id中的post_id链接在一起的列post_id。

然后您可以通过此扩展查询一次检索所有信息。

$post_user = $this->db->query("SELECT posts.post_title, posts.post_content, posts.post_date, users.username FROM posts LEFT JOIN users ON posts.user_id = users.user_id LEFT JOIN category ON category.category_id = post.category_id ORDER BY posts.post_ID DESC"); 

在这里你得到了类别。

对于评论,我不知道你想如何输出。 Yan说,一种方法是。如果你可以更具体,我可以建议一些更简单的方法。