微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CakePHP增量值

我的问题如下所示:

我想制作一个投票应用程序,以便人们可以选择一个或多个事件(例如Doodle).
为此,我设置了一个称为表决的功能.在视图中,您可以选择事件
使用复选框.模型是民意调查和小组活动.投票有许多Groupevent.
我的问题是,当我调用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.');
        }
    }
}

如何使它起作用,以便仅使检查值递增?

提前致谢

编辑:

感谢您对阵列的建议.但我已经尝试过某些无法使用的方式.如何创建此阵列?

解决方法:

似乎您正在为分配给选定民意调查的所有Groupevents运行updateall查询

if ($this->Poll->Groupevent->updateall(
              array('Groupevent.Votes'=>'Groupevent.Votes+1'), 
              array('Groupevent.poll_id'=>$this->Poll->id))) 
        { 
...

您需要创建一个具有选中的Groupevents的ID的数组,并添加一个条件,该条件将仅将更新限制为选定的事件:

if ($this->Poll->Groupevent->updateall(
              array('Groupevent.Votes'=>'Groupevent.Votes+1'),
              array('Groupevent.id IN' => $selectedEventsIds),
              array('Groupevent.poll_id'=>$this->Poll->id))) 
        {
...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐