CakePHP-2.0: рефакторинг моего кода, ускорение SQL-запросов, требуется более быстрый SQL-запрос - PullRequest
0 голосов
/ 17 декабря 2011
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?

1 Ответ

1 голос
/ 17 декабря 2011

Попробуйте это

$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);
}
...