2009-10-21 59 views
0

我有兩個表:分頁與協會的hasMany CakePHP的

選手和投票 參賽者的hasMany投票

我試着做一個計數(Vote.id)的投票,所以我可以將它放在 記錄集和分頁他們,但我不知道該把它放在哪裏。 我在字段數組上做了它,但它給了我總票數 ,無論他們屬於哪個參賽者。

的票參賽者記錄鏈接在一起所以我 所做的是對我的看法,我做了計數($選手[投票]),但是這不能成爲 分頁和我的客戶希望能夠進行排序參賽者由 投票。

有沒有一種方法可以讓我做這樣的事情對我的看法?:

排序(「投票」,「計數(投)」); ?>

或者我是否必須創建一個查詢,對所有選票進行計數 其中Contestant.id = Votes.contestant_id?

控制器參賽者:

function index() { 
      $page = 'Contestants'; 
      $this->set('page', $page); 
      $this->paginate = 
      array(
        'order' => 'id ASC', 
        'contain' => array(
          'Vote' => array(
            'fields' => array("Vote.contestant_id",'Vote.id') 
          ) 
        ) 
      $conditions ["Contestant.active"] = 1; 
      $this->set('contestants', $this->paginate('Contestant', 

$條件)); }

回答

0

從CakePHP不通過中容納的行爲支持組我嘗試了不同的方法。創建投票模式,而不是(所有這一切都是在參賽者控制器來完成的)PAGINATE VAR:

var $paginate = array(
    'Vote'=>array(
     'limit'=>5, 
     'fields' => array(
     'Contestant.*, count(Vote.contestant_id) as Contestant_votes, Vote.id' 
     ), 
     'group' => array(
     'Vote.contestant_id' 
     ), 
     'order' => array(
     'Contestant_votes Desc' 
     ) 
    ), 
    'Contestant'=>array(
     'limit'=>5, 
     'order' => array(
     'Contestant.id Desc' 
     ) 
    ) 
); 

現在在我的控制器我做到以下幾點:

function index() { 
    $page = 'Contestants'; 
    $this->set('page', $page); 
    $conditions ["Contestant.active"] = 1; 
    $this->set('contestants', $this->paginate($this->Contestant->Vote,$conditions)); 
} 

現在的選手是有序儘管我仍然無法確定如何將Contestant_votes作爲paginator變量,因爲在記錄集中它是在它自己的數組中,而不是在用於分頁的任何模型數組中。

感謝馬特哈金斯你的方法是讓我找到這個解決方案的人。

0

查覈在這個問題deceze的迴應:CakePHP mathematic-calculation field?

基本上你想要做這樣的事我猜測:

'contain' => array(
    'Vote' => array(
     'fields' => array('SUM(Vote.id) AS Contestant__votes'), 
     'group' => array('Vote.contestant_id'), 
    ) 
) 
+0

奇怪,當我嘗試你的建議,這是它建立查詢。 查詢:SELECT SUM('Vote'.'id')AS Contestant__votes,'Vote'.'group','Vote'.'contestant_id' FROM'votes' AS'Vote' WHERE'Vote'.'contestant_id 'IN(1,2) – 2009-10-21 19:28:15

+0

它顯示此錯誤:模型「投票」不與模型「投票」相關聯。這是一個錯誤?爲什麼需要將模型與自身相關聯? – 2009-10-21 20:03:36

+0

好吧似乎CakePHP的不通過中可容納的行爲支持組,所以我做了這樣的事情: '條件'=> 「1 = 1 GROUP BY Vote.contestant_id」 並生成該SQL查詢: SELECT SUM (Vote'.'id')AS Contestant__votes,'Vote'.'contestant_id' FROM'votes' AS'Vote' WHERE 1 = 1 GROUP BY'Vote'.'contestant_id' AND'Vote'.'contestant_id' IN( 1,2) – 2009-10-21 20:53:48

0

此外:您是否也想按票數(總票數)升序或降序排序?如果是的話,你不能用cakephp的默認分頁方法輕鬆完成。

爲此,您需要稍微調整一下。這裏是關於這個技巧的細節:CakePHP Advanced Pagination – sort by derived field

希望你會發現它有幫助。

感謝 阿德南

0

爲您定義的特定關係,您的需求是由counter-caching服務良好。

您需要在contestants表中定義一個新字段:vote_count。然後,在你的投票模式,你需要稍微更新$belongsTo定義:

class Votes extends AppModel 
{ 
    var $belongsTo = array(
     'Contestant' => array('counterCache' => true) 
    ); 
} 

現在,每當有投票記錄保存,父參賽者的vote_count字段將被更新。現在,你可以簡單地排序Contestant.vote_count,就像任何其他選手現場:

class ContestantsController extends AppController 
{ 
    // Controller stuff that comes before... 

    function index() 
    { 
     $this->paginate = array('Contestant' => array(
      'conditions' => array('Contestant.active' => 1), 
      'order' => array('Contestant.vote_count' => 'DESC'), 
     )); 

     $contestants = $this->paginate('Contestants'); 

     $this->set(compact('contestants')); 
    } 

    // Controller stuff that comes after... 
}