CakePHP increment value

后端 未结 2 1765
粉色の甜心
粉色の甜心 2021-01-25 04:56

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 Vi

相关标签:
2条回答
  • 2021-01-25 05:00

    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))) 
            {
    ...
    
    0 讨论(0)
  • 2021-01-25 05:03

    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

    0 讨论(0)
提交回复
热议问题