4

私はこれをモデルに持っています:

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が必要ですが、モデルからすべてのクエリを実行した場合、どうすれば取得できますか?コメントも同じ

大きくて複雑なDBクエリを1つ作成するのは良いことですか(難しいですが、実行できます)、または2つまたは3つの別々の小さなクエリを実行できますか?

4

2 に答える 2

2

Codeigniterを使用すると、データベースの結果をオブジェクト(たとえば、モデルオブジェクト)として返すことができます。これにより、データの操作がはるかに簡単になります。Postsテーブルに最初のクエリを発行posts.idし、結果セットにフィールドを含め、Post_modelクラスの名前を$db->query->result()関数に渡して、結果をクラスのインスタンスとして返すことをcodeigniterに通知できますPost_model

Post_model次に、クラスのメソッドをGetCategoriesbypost_idおよびGetCommentsbyで定義し、post_idこれらのメソッドを呼び出して、クエリから返され たそれぞれのコレクション$categoriesとコレクションにデータを入力できます。$commentsPost_model

ここに例があります、私はそれが役立つことを願っています:

    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;
        }    
}

次に、ビューで、result_arraysとしてではなくPost_modelオブジェクトとして$posts配列にアクセスできます。

<?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 } ?>

効率の問題に関しては、それは多くの要因に依存します(データベースはWebサーバーと同じマシンにありますか?投稿はいくつありますか?など)。通常、1つの大きなクエリは、いくつかの小さなクエリよりも高速に実行されますが、パフォーマンスの向上を求める価値があるかどうかを実際に判断するには、プロファイリングが必要になります。私は常に、複雑さを増すことを犠牲にして最適化するよりも、読みやすく理解しやすいコードを書こうとすることを好みます。

于 2012-04-16T07:52:36.040 に答える
0

できることは1つ。

別のカテゴリ テーブルとコメント テーブルを作成します。

category_id は、投稿テーブルの category_id 列とリンクされます。

コメント テーブルで、post テーブルの 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");

ここにカテゴリがあります。

コメントについては、出力をどのようにしたいかわかりません。1つの方法は、ヤンが言ったとおりです。もっと具体的に言えば、もっと簡単なアプローチをアドバイスできます。

于 2012-04-14T15:59:49.910 に答える