【问题标题】:CakePHP-2.0: Refactor my code editing faster sql query, need a faster sql queryCakePHP-2.0:重构我的代码编辑更快的sql查询,需要更快的sql查询
【发布时间】:2011-12-17 16:21:25
【问题描述】:
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)

我需要具有更多 cmets 限制 10 或没有限制的 posts.title。

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

        $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 查询以查找带有更多 cmets 的 posts.it、posts.title 吗?还是有 cakephp 的 find 命令?

【问题讨论】:

    标签: mysql database cakephp cakephp-2.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);
    }
    

    【讨论】:

    • (count($post['Comment']) &gt;= 10); 还是 (count($posts['Comment']) &gt;= 10); ,问题是通过带有该 post_id 的 count(comment) DESC 获取 posts.title 顺序。
    • array_filter 将为指定为第一个参数的数组的每个元素运行一次指定的函数(在本例中为 ten_or_more)。 $post 用于引用正在检查的 $posts_with_cmets 数组的当前元素,实际上并不存在于其他任何地方。如果你想按 cmets 的数量排序,我们需要添加一个 uasort 来对数组进行排序,但我觉得这可以通过 cake find 更好地实现,我只是不知道如何。
    猜你喜欢
    • 2015-06-05
    • 2012-06-29
    • 2010-11-20
    • 1970-01-01
    • 2020-01-30
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多