2012-04-14 30 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說,一種方法是。如果你可以更具體,我可以建議一些更簡單的方法。