2015-07-21 81 views
0

如何計算查看量以便我可以根據流行度進行顯示,即誰擁有最高查看次數以便先顯示等。哪個場所的評價最高,它會先顯示,然後是其他顯示。我已經嘗試過,但它不起作用。CakePHP SQL查詢計數以獲得最大查看量

Array 
(
    [0] => Array 
     (
      [Venue] => Array 
       (
        [id] => 6 
       ) 
      [Review] => Array 
       (
        [0] => Array 
         (
          [id] => 9 
          [rating] => 4 
          [venue_id] => 6 
         ) 
        [1] => Array 
         (
          [id] => 10 
          [rating] => 3 
          [venue_id] => 6 
         ) 
        [2] => Array 
         (
          [id] => 11 
          [rating] => 3 
          [venue_id] => 6 
         ) 
       ) 
     ) 

    [1] => Array 
     (
      [Venue] => Array 
       (
        [id] => 15 
       ) 
      [Review] => Array 
       (
        [0] => Array 
         (
          [id] => 16 
          [rating] => 5 
          [venue_id] => 15 
         ) 
        [1] => Array 
         (
          [id] => 17 
          [rating] => 4 
          [venue_id] => 15 
         ) 
       ) 
     ) 
    [2] => Array 
     (
      [Venue] => Array 
       (
        [id] => 17 
       ) 
      [Review] => Array 
       (
        [0] => Array 
         (
          [id] => 15 
          [rating] => 4 
          [venue_id] => 17 
         ) 
       ) 
     ) 
    [3] => Array 
     (
      [Venue] => Array 
       (
        [id] => 31 
       ) 
      [Review] => Array 
       (
       ) 
     ) 
    [4] => Array 
     (
      [Venue] => Array 
       (
        [id] => 32 
       ) 
      [Review] => Array 
       (
        [0] => Array 
         (
          [id] => 18 
          [rating] => 1 
          [venue_id] => 32 
         ) 
        [1] => Array 
         (
          [id] => 19 
          [rating] => 1 
          [venue_id] => 32 
         ) 
        [2] => Array 
         (
          [id] => 20 
          [rating] => 1 
          [venue_id] => 32 
         ) 
        [3] => Array 
         (
          [id] => 21 
          [rating] => 1 
          [venue_id] => 32 
         ) 
        [4] => Array 
         (
          [id] => 22 
          [rating] => 1 
          [venue_id] => 32 
         ) 
       ) 
     ) 
) 

     if ($order == 'popularity') { 
      $order = array('COUNT(Review) DESC'); 
     } 
     $this->Paginator->settings = array('conditions' => $conditions, 'limit' => 10, 'order' => $order, 'page' => @$data['page']); 
     $sqql = $this->Paginator->paginate('Venue'); 
+1

閱讀說明書? http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-count另外* always *標記您正在使用的特定CakePHP版本。 – burzum

+0

歡迎來到Stack Overflow!我編輯你的問題來修復一些語法並改進格式。我還將代碼示例縮進了4個空格,以便它能夠正確呈現。我刪除了許多備用空行 - 請參閱編輯幫助以獲取有關格式化的更多信息。請編輯以提供確定具體問題所需的其他詳細信息。祝你好運! – Wtower

回答

0

在這裏,我做了簡單地寫在給定控制器的型號。我發現這個使用CakePHP的「Virtualfield」的概念做到這一點的最好辦法2線路編碼。

var $virtualFields = array(
    'review_count' => 'SELECT COUNT(*) FROM reviews as Review WHERE Review.venue_id = Venue.id' 
); 

使用Virtualfield概念後,它似乎太容易了。

這裏的結果會像這樣

Array 
(
    [0] => Array 
     (
      [Venue] => Array 
       (
        [id] => 6 
        [review_count]=> 3 
       ) 
     ) 
[1] => Array 
     (
      [Venue] => Array 
       (
        [id] => 15 
        [review_count]=>2 
       ) 
     ) 
[2] => Array 
     (
      [Venue] => Array 
       (
        [id] => 17 
        [review_count]=>1 
       ) 
     ) 
[3] => Array 
     (
      [Venue] => Array 
       (
        [id] => 31 
        [review_count]=>0 
       ) 
     ) 
[4] => Array 
     (
      [Venue] => Array 
       (
        [id] => 32 
        [review_count]=>5 
       ) 
     ) 

在這裏,人們可以很容易找到的評論的一定的id nunber。 謝謝