0

大家好,当试图更新我的数据库时,这个函数没有从表中获取 id,也没有更新表中的那一行。它也抛出一个错误

$this->Relationship->id = $this->request->data['id'];

这是完整的功能

public function approve($id=null){
        $this->Relationship->id = $id;
        if($this->request->is('get')){
        $this->request->data=$this->Relationship->read();}
        $this->Relationship->id = $this->request->data['id'];
        if($this->Relationship->save($this->request->data)) {
        $this->Session->setFlash('Your Relationship has been updated.');
        $this->redirect(array('action' => 'request'));
        } else {
            $this->Session->setFlash('Unable to update your post.');
        }
    }
    }

这是表格/视图

<?php
echo $this->Form->create('Relationship', array('action'=>'approve'));
echo $this->Form->input('expirydate',array('label'=>'Expiry Date: ', 'class' => 'dateclass')); 
echo $this->Form->end('Submit');

?>

我试图用这个函数做的是获取一个 id,并编辑该条目中的两个字段

4

2 回答 2

0

$this->request->data['Relationship']['id'];如果您正确设置表单,应该是这样。另外,你可以做

$this->Relationship->create($this->request->data);
$this->Relationship->save()
于 2012-05-26T05:52:13.483 回答
0
    public function approve($id=null){
        $this->set('title_for_layout', 'Relationships');
        $this->set('stylesheet_used', 'homestyle');
        $this->set('image_used', 'eBOXLogoHome.jpg');   
        $this->layout='home_layout';

        if ($this->request->is('get')) {
        $this->request->data = $this->Relationship->read(NULL, $id);

        } else {
        //sets active to 1
         $this->Relationship->read(null, $id);
         $this->Relationship->set(array('active' => true,));
        if ($this->Relationship->save($this->request->data)) {

            $this->Session->setFlash('Your post has been updated.');
            $this->redirect(array('action' => 'request'));
        } else {
            $this->Session->setFlash('Unable to update your post.');
        }
    }
}

我还必须在数据库中将 tinyint 从 0=>1 更改。我遇到的另一个问题是我的请求视图,它没有将 id 传递给批准函数。一旦我将代码更改为此它就可以工作

<?php foreach($Relationships as $relationship):?>
                    <tr> 

                        <td align='center'><?php echo $relationship['Relationship']['partyone']; ?></td>
                        <td align='center'><?php echo $relationship['Relationship']['partytwo']; ?></td>
                        <td> </td>
                        <td><?php echo $this->Html->link($relationship['Relationship']['partyone'], array('action'=>'approve', $relationship['Relationship']['id'])); ;?>
                        </td>

                    </tr>
                <?php endforeach; ?>

            </table>
于 2012-05-26T13:48:40.350 回答