SQL更新语句中缺少cakephp post数据字段

时间:2012-12-04 17:21:16

标签: cakephp cakephp-2.1

无法使用cakephp来更新记录。

控制器代码:

public function viewBenefit($id) {
    if ($this->request->is('post')) {
        $this->set('post', $this->request->data);
        $this->Benefit->id = $id;
        if ($this->Benefit->save($this->Benefit->data)) {
            $myVars['Sucsess'] = TRUE;
            $this->Session->setFlash('Updates Saved');
        } else {
            $myVars['NewID'] = 0;
            $myVars['Sucsess'] = False;
            $this->Session->setFlash('There was an error.');
        }
    }

    $this->Benefit->recursive = 2;
    $this->Benefit->id = $id;
    $this->set('benefit', $this->Benefit->read());
}

相关观看代码:

<?php echo $this->Form->create('Benefit',array('action'=>'edit','url' => '#')); ?>
<?php echo $this->Form->input('id',array('type'=>'hidden')) . "\n"; ?>
<?php echo $this->Form->input('short_description',array('type'=>'textarea')) . "\n"; ?>
<?php echo $this->Form->end(); ?>

注意:表格通过JS

进行汇总

POST数据(通过debug($ post);)

array(
    'Benefit' => array(
        'id' => '1952e98e-f589-47d4-b458-11a1bd58ba3b',
        'short_description' => '<p>This is great sample insurance 321321</p>'
    )
)

SQL UPDATE语句:

UPDATE `c16memberdev`.`benefits` SET `modified` = '2012-12-04 10:45:16' WHERE `c16memberdev`.`benefits`.`id` = '1952e98e-f589-47d4-b458-11a1bd58ba3b'

正如您所看到的,“short_description”字段未添加到SQL语句中,因此未添加到数据库中的数据。谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

尝试更改

$this->Benefit->save($this->Benefit->data)

$this->Benefit->save($this->request->data)