CakePHP 2.0:我如何使用自己的功能

时间:2014-05-14 11:29:46

标签: sorting cakephp calculated-field

我使用CakePHP 2.0。我做了一个新的函数排序,没有问题,如果我拿表格字段。但是如果我想在我的排序方法中计算某些东西并给出结果(方法:'Participant.year'=>'calculateyear'),我的观点就没有结果了。

function sort() { 
    $participants = $this->Participant->find('all', array(
    'conditions'=>array('Participant.sex'=>'m','Participant.year'=>'calculateyear'),
    'order'=>array('Participant.communitieRank'=>'ASC','Communitie.name'=>'ASC')
    ));
    $this->set('participants', $participants);  
  }

  function calculateyear ($jahr) {
      $jahr = '2000'; 
      return $jahr;
 }

2 个答案:

答案 0 :(得分:1)

假设你在PartecipantsController中,我猜你想做的是:

function sort() { 
    $participants = $this->Participant->find('all', array(
    'conditions'=>array(
        'Participant.sex'=>'m',
        'Participant.year'=> $this->calculateyear(2000)
    ),
        'order'=>array('Participant.communitieRank'=>'ASC','Communitie.name'=>'ASC')
    ));
    $this->set('participants', $participants);  
  }

但你的问题根本不清楚

答案 1 :(得分:0)

这就是我的解决方案,它有效:)

function calculateyear ($year) {
     $today = date("Y");
     $year = $today - $year;
      return  $year;
  }  

  function kat1w() { 
    $participants = $this->Participant->find('all', array(
        'conditions'=>array('Participant.sex'=>'f',
       'Participant.year >='=>$this->calculateyear(9)),
       'order'=>array('Participant.communitieRank'=>'ASC','Communitie.name'=>'ASC')
    ));
    $this->set('participants', $participants,$this->Paginator->paginate());  
  }