1

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?

blake305
  • 2,196
  • 3
  • 23
  • 52
ulanBator
  • 45
  • 1
  • 6

2 Answers2

4

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))) 
        {
...
bbb
  • 702
  • 3
  • 6
  • hi, thanks for the reply. But I cannot create an array from this data. I tried $this->data['Groupevent'] and $this->data['Groupevent']['id'] and some others but it didn't work. – ulanBator Jan 07 '12 at 23:46
  • Could you please paste a dump of print_r($this->data) after the submit? – bbb Jan 08 '12 at 11:10
  • Strange. How about checking $_POST variable? This is not very Cakeish approach, but nevertheless fine for debugging purposes... – bbb Jan 08 '12 at 18:32
  • finally it works. I was just trying different approaches till it worked. thanks for your help! – ulanBator Jan 08 '12 at 20:41
0

I have an app I am developing where I allow users(thru jQuery) to Vote UP or Down a specific comment. This is how I implemented it. I basically call this function thru Ajax/jQuery and it only increments the Comment by the passed argument; My comments table has 2 fields - Liked and Disliked. One example if voteUp and I also have voteDown which is pretty similar.

function voteUp($id = null){

    $this->autoRender = false; 
    if($this->RequestHandler->isAjax()){
        $this->Comment->id = $id;
        // Basically I get the value of liked(Field in Db) and increment it by 1
        $this->Comment->saveField('liked',$this->Comment->field('liked')+1);
    }       
    $newValue =  $this->Comment->findById($id);     
    //pass this value back to the view so it is displayed
    //using jQuery
    return $newValue['Comment']['liked'];
}

Let me know if you have anymore questions

AKKAweb
  • 3,795
  • 4
  • 33
  • 64
  • 3
    Just be careful of doing it this way as on a high-traffic site you might get simultaneous requests that occasionally overwrite each others data and counts get missed (for critical situations like payments, this is not good). It's better to use `$this->Model->updateAll(['x' => 'x + 1'], ['id' => 1]);` which should handle these situations more safely. – Simon East Dec 20 '12 at 06:20