【发布时间】:2012-01-07 22:01:30
【问题描述】:
我的问题如下:
我想制作一个投票应用程序,人们可以在其中选择一个或多个事件(如涂鸦)。
为此,我设置了一个名为 vote 的函数。在视图中,您可以选择事件
使用复选框。模型是 Poll 和 Groupevent。民意调查有许多 Groupevents。
我的问题是当我调用updateAll() 时,关联的 Groupevents 的所有值都是
递增。
这是我的代码:
查看:
echo $this->Form->create('Poll', array('action' => 'vote'));
for($i=0; $i<$count; $i++){
echo $this->Form->input('Groupevent.'.$i.'.votes', array('type'=>'checkbox',
'label'=>$this->Groupevent['startTime']));
echo $this->Form->input('Groupevent.'.$i.'.id', array('type'=>'hidden'));
}
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->end('vote');
控制器功能:
function vote($id=null){
$this->Poll->id = $id;
if ($this->request->is('get')) {
$this->request->data = $this->Poll->read();
$this->set('count', $this->Poll->Groupevent->find('count',
array('conditions'=>array('Groupevent.poll_id'=>$this->Poll->id))));
} else {
unset($this->Poll->Groupevent->validate['poll_id']);
if ($this->Poll->Groupevent->updateAll(
array('Groupevent.votes'=>'Groupevent.votes+1'),
array('Groupevent.poll_id'=>$this->Poll->id)))
{
$this->Session->setFlash('Your poll has been updated.');
$this->redirect(array('action' => 'edit', $id));
} else {
$this->Session->setFlash('Unable to update your poll.');
}
}
}
我怎样才能让它工作,以便只增加检查的值?
提前致谢
编辑:
感谢您对数组的建议。但我已经尝试了某些方法它不会工作。如何创建这个数组?
【问题讨论】:
标签: php cakephp has-many increment update-all