如何使用CakePHP更新一行上的多个字段

时间:2014-04-02 15:13:46

标签: mysql database cakephp updates

我试图在数据库的表上更新一行的多个字段。我尝试了一些我在stackoverflow中找到的解决方案,但没有人为我工作过。 在此功能中,我提供了产品'的反馈,在产品表I中有4个字段,分别为num_votes,num_negative_votes,num_neutral_votes和num_positive_votes。 当我调用此函数时,我需要根据表单的值更新数据库的这些字段。

如何同时更新2个字段?

我尝试的解决方案就是这个: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'));
        }
    }

1 个答案:

答案 0 :(得分:2)

试试这个 -

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


}