CakePHP-2.0:重构我的代码编辑速度更快的sql查询,需要更快的sql查询

时间:2011-12-17 16:21:26

标签: mysql database cakephp cakephp-2.0

mysql> describe posts;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| user_id     | int(11)      | NO   |     | NULL    |                |
| title       | varchar(255) | NO   |     | NULL    |                |
| body        | text         | YES  |     | NULL    |                |
| category_id | int(11)      | NO   |     | NULL    |                |
| tags        | varchar(50)  | NO   |     | NULL    |                |
| mark        | tinyint(4)   | NO   |     | 1       |                |
| created     | datetime     | YES  |     | NULL    |                |
| modified    | datetime     | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)

mysql> describe comments;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| post_id  | int(11)      | NO   | MUL | NULL    |                |
| name     | varchar(255) | NO   |     | NULL    |                |
| email    | varchar(255) | NO   |     | NULL    |                |
| body     | varchar(500) | NO   |     | NULL    |                |
| mark     | tinyint(4)   | NO   |     | 1       |                |
| created  | datetime     | YES  |     | NULL    |                |
| modified | datetime     | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

我需要posts.title,它有更多的评论限制10或没有限制。

到目前为止我尝试了什么=>

        $conditions=array(
            'fields'=>array('Comment.post_id'),
            'group'=>array('Comment.post_id'),
            'order'=>array('count(Comment.post_id) DESC'),
            'limit'=>'5'
        );          
        $mostComments=$this->Post->Comment->find('all',$conditions);

        $postId=array();
        foreach($mostComments as $val){
            array_push($postId,$val['Comment']['post_id']);
        }
        $postsWithmostComments=$this->Post->find('all',array('conditions'=>array('Post.id '=>$postId)) );
        $this->set('postsWithmostComments',$postsWithmostComments);

任何人都可以发布sql查询来查找posts.it,posts.title以及更多评论吗?或者任何cakephp find命令?

1 个答案:

答案 0 :(得分:1)

试试这个

$contain = array('Comment');

$posts = $this->Post->find('all', array('contain'=>$contain));

$posts_with_comments = array_filter($posts, 'ten_or_more');
uasort($posts_with_comments, 'order_by_comment_count');    

$this->set('postsWithmostComments',$posts_with_comments);

function order_by_comment_count($a, $b) {
    if (count($a['Comment'] == $b['Comment']) 
       return 0;
    return ($a['Comment'] < $b['Comment']) ? -1 : 1;
}

function ten_or_more($post) {
   return (count($post['Comment']) >= 10);
}