我正在尝试编辑数据,但此处此编辑数据将是一个新条目,旧数据将被删除。
所以我试过下面的代码来做这个
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.'));
}
}
此处仅发生删除我没有获得新的条目数据。我怎样才能做到这一点?愿有人帮帮我吗?
答案 0 :(得分: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
答案 1 :(得分: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);
}
我觉得它可以帮到你。