2009-10-21 168 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... 
}