CakePHP增量值 [英] CakePHP increment value

查看:125
本文介绍了CakePHP增量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如下:



我想进行投票应用程式,人们可以选择一个或多个活动(例如Doodle)。
为此,我设置了一个名为vote的函数。在视图中,您可以使用复选框选择事件
。模型是Poll和Groupevent。一个投票有很多Groupevents。
我的问题是当我调用 updateAll()时,相关联的Groupevent的所有值都是
递增。



这是我的代码:



查看:

  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 .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('无法更新您的调查。
}
}
}



编辑:



感谢数组的建议。但我试过某些方法,它不工作。如何创建此数组?

解决方案

看起来您正在运行 updateAll 查询分配给所选择的投票的所有Groupevent:

  if($ this-> Poll-> Groupevent- > updateAll(
array('Groupevent.votes'=&'; Groupevent.votes + 1'),
array('Groupevent.poll_id'=> $ this-> Poll-> id)))
{
...

数组,其中包含已检查的Groupevent的ID,并添加一个条件,只将更新限制到选定的事件:

  if > Group> Groupevent-> updateAll(
array('Groupevent.votes'=>'Groupevent.votes + 1'),
array('Groupevent.id IN'=& $ selectedEventsIds),
array('Groupevent.poll_id'=> $ this-> Poll-> id)))
{
...


My problem looks as follows:

I want to make a vote app where People can choose one or more Events(like Doodle). For this I have set up a function called vote. In the View you can choose the Events using checkboxes. The Models are Poll and Groupevent. A Poll has Many Groupevents. My Problem is when I call updateAll(), all values of the associated Groupevents are incremented.

Here is my code:

View:

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');

Controller function:

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.');
        }
    }
}

How can I make it work, so that just the checked values get incremented?

thanks in advance

Edit:

Thanks for the suggest with the array. But i've tried certain ways it wont work. How do I create this Array?

解决方案

It looks like you are running an updateAll query for all Groupevents assigned to a selected Poll:

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

You need to create an array with the ID's of checked Groupevents and add a condition that will limit the update only to selected events:

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

这篇关于CakePHP增量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆