【问题标题】:How to update multiple fields on one row with CakePHP如何使用 CakePHP 更新一行中的多个字段
【发布时间】:2014-04-02 15:13:46
【问题描述】:

我正在尝试更新数据库表中一行的多个字段。我尝试了几种我在 stackoverflow 中找到的解决方案,但没有人为我工作。 在这个函数中,我给出了一个“产品”的反馈,在产品表中我有 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'));
        }
    }

【问题讨论】:

    标签: mysql database cakephp updates


    【解决方案1】:

    试试这个-

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

    【讨论】:

    • 天哪,它有效!非常感谢:_)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    相关资源
    最近更新 更多