Julia
Julia

Reputation: 558

How to update multiple fields on one row with CakePHP

I'm trying to update multiple fields of a row on a table on the database. I tried several solutions I found here in stackoverflow but no one had worked for me. In this function, I give a feedback of a 'Product', and in the table Product I have 4 fields, called num_votes, num_negative_votes, num_neutral_votes and num_positive_votes. When I call this function, i need to update this fields of the database depending on the value of the form.

How can I update 2 fields at the same time?

The solution I tried is this one: CakePHP - How to update multiple records

public function setFeedback($id = null) {
        $this->autoRender = false;
        if (!$id) {
                $this->redirect(array('action' => 'index'));
        }

        else {
            $product = $this->Product->findById($id);
            $num_votes = $product['Product']['num_votes'] + 1;
            if($this->request->data['Product']['num_points'] == "0") {
                $num_negative_votes = $product['Product']['num_negative_votes'] + 1;
                $arrayToSave = array(
                    'num_votes' => $num_votes,
                    'num_negative_votes' => $num_negative_votes);
                $this->Product->saveMany($arrayToSave, array('deep' => true));
            }

            else if ($this->request->data == "1") {
                $num_neutral_votes = $product['Product']['num_neutral_votes'] + 1;
                $arrayToSave = array(
                    'num_votes' => $num_votes,
                    'num_neutral_votes' => $num_neutral_votes);
                $this->Product->saveMany($arrayToSave, array('deep' => true));
            }

            else if ($this->request->data == "2 ") {
                $num_positive_votes = $product['Product']['num_positive_votes'] + 1;
                $arrayToSave = array(
                    'num_votes' => $num_votes,
                    'num_positive_votes' => $num_positive_votes);
                $this->Product->saveMany($arrayToSave, array('deep' => true));
            }
            $this->redirect(array('action' => 'index'));
        }
    }

Upvotes: 0

Views: 4881

Answers (1)

Fazal Rasel
Fazal Rasel

Reputation: 4526

Try this-

public function setFeedback($id = null) {
        $this->autoRender = false;
        if (!$id) {
                $this->redirect(array('action' => 'index'));
        }


            $product = $this->Product->findById($id);
            $num_votes = $product['Product']['num_votes'] + 1;

            if($this->request->data['Product']['num_points'] == "0") {
                $num_negative_votes = $product['Product']['num_negative_votes'] + 1;
                $arrayToSave['Product']['num_negative_votes'] = $num_negative_votes;
            }

            else if ($this->request->data == "1") {
                $num_neutral_votes = $product['Product']['num_neutral_votes'] + 1;
                $arrayToSave['Product']['num_neutral_votes'] = $num_neutral_votes;
            }

            else if ($this->request->data == "2 ") {
               $num_positive_votes = $product['Product']['num_positive_votes'] + 1;
               $arrayToSave['Product']['num_positive_votes'] = $num_positive_votes;
            }


            $arrayToSave['Product']['num_votes'] = $num_votes;

            $this->Product->id = $id;
            if($this->Product->save($arrayToSave)){
                $this->redirect(array('action' => 'index'));
            }else{
                $this->Session->setFlash('Something is wrong.');
            }           


}

Upvotes: 2

Related Questions