我使用CakePHP 2.0。我创建了一个新的函数sort,如果我取表字段,就没有问题了。但是如果我想计算一些东西并给出结果(方法:'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;
}
假设你是在你的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);
}
但是你的问题一点也不清楚
这是我的解决方案,它有效:)
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());
}