从CodeIgniter(或任何MVC平台)中的数据库中提取数据?

时间:2012-04-14 11:57:59

标签: php mysql sql model-view-controller codeigniter

我在模型中有这个:

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个单独的小查询是不是很好?

2 个答案:

答案 0 :(得分:2)

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

然后,您可以Post_model类将方法定义为GetCategories post_idGetComments post_id,然后调用这些方法来填充{从您的查询返回的每个$categories的{​​1}}和$comments个集合。

这是一个例子,我希望它有所帮助:

Post_model

然后,在您的视图中,您可以将$ posts数组作为Post_model对象而不是result_arrays访问:

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

至于效率问题,它将取决于很多因素(数据库与网络服务器位于同一台机器上?有多少帖子?等等)。单个大型查询通常比几个较小的查询执行速度快,但需要进行性能分析才能真正确定性能增益是否值得寻求。我总是喜欢尝试编写可读/可理解的代码,而不是以增加复杂性为代价进行优化。

答案 1 :(得分:0)

你可以做一件事。

制作不同的类别表和评论表。

category_id将与帖子表中的category_id列相关联。

在评论表中,创建列post_id,它将与post表中的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");

这里有你的类别。

对于评论,我不确定您希望输出如何。正如严说的那样。如果你可以更具体,我可以建议一些更简单的方法。