【发布时间】: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