0

我正在尝试编辑数据,但这里编辑数据将是一个新条目,我们将删除旧数据。

所以我尝试了下面的代码来做到这一点

public function edit($id = null){
if ($this->request->is(array('post', 'put')))
{
    $this->Transaction->create();
    if ($this->Transaction->save($this->request->data)) {
                $this->Transaction->delete();
                $this->Session->setFlash(__('The transaction has been saved.'));
    }

}

这里只发生了删除我没有得到新的条目数据。我怎样才能做到这一点?有人可以帮我吗?

4

2 回答 2

0

在表单编辑中,首先将您的 id 分配给模型主键。并且需要形成 create() 方法,并检查您的视图源 PUT 方法是否可用。

 public function edit($id = null){
 if (!$this->Transaction->exists($id)) {
        throw new NotFoundException(__('Invalid user'));
    } 
 if ($this->request->is(array('post', 'put'))){
   $this->Transaction->id=$id;
   if ($this->Transaction->save($this->request->data)) {   
            $this->Session->setFlash(__('The transaction has been saved.'));
    }
  }

另一种替代方法是 updateAll() Cakephp updateAll

于 2016-01-25T04:58:58.777 回答
0

有很多方法可以做到这一点。让我们看看一种方式

public function edit(){

    if ($this->request->is(array('post', 'put')))
    {
        $this->Transaction->delete($this->request->params['pass'][0]);  // after post at first delete your entry.

        if ($this->Transaction->save($this->request->data)) {
                    $this->Session->setFlash(__('The transaction has been saved.'));
        }

    }else {
        //to get this id data.
        $id = $this->request->params['pass'][0];
        $options = array('conditions' => array('Transaction.' . $this->Transaction->primaryKey => $id));
        $this->request->data = $this->Transaction->find('first', $options);
    }

我认为它可以帮助你。

于 2016-01-25T05:56:13.553 回答