【问题标题】:Cakephp Edit function is not working if($this->Category->save($this->request->data))Cakephp 编辑功能不工作 if($this->Category->save($this->request->data))
【发布时间】:2015-04-13 04:55:47
【问题描述】:

我在数据库中有两列,表类别下的 id 和 name。 我的编辑功能:

public function edit($id = null){
    if($this->Category->exists($id)){
        throw new NotFoundException (__('Id was not found '));
    }
    if($this->Auth->user('role')=='admin'){
    if(!$id){
        throw new NotFoundException(__('Id was not set'));
    }

    $data=$this->Category->findById($id);
    if(!$data){
        throw new NotFoundException(__('Id was not found in database'));
    }

   if($this->request->is(array('post','put')) ){

        if($this->Category->save($this->request->data)){
            $this->Session->setFlash('The data has been edited successfully');
            return $this->redirect(array('action' => 'index'));
            //$this->redirect('index');
        }

        else{
            $this->Session->setFlash("The data could not be edited");
            $this->redirect(array('controller'=>'categories','action'=>'index'));
        }
    }
//        else{
//            $options = array('conditions' => array('Category.' . 
 $this->Category->primaryKey => $id));
//          $this->request->data = $this->Category->find('first', $options);
//        }
    $this->request->data=$data;
}
else{
    $this->Session->setFlash(__('You do not have right to do this'));
    $this->redirect('index');
}
   }

我的类别/edit.ctp

 <?php echo $this->Form->create('Category'); ?>
<fieldset>
    <legend><?php echo __('Edit Category'); ?></legend>
<?php
    echo $this->Form->input('id');      
    echo $this->Form->input('name');        
            echo $this->Form->input('id',array('type'=>'hidden'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>

每当我尝试编辑和更新表单时, $this->Session->setFlash("无法编辑数据");被执行。

【问题讨论】:

  • 您是否在编辑中打印了请求值?使用 pr($this->request->data) 并检查。同时启用你的 debig 模式,看看是否有任何错误显示
  • 需要更多优化,一旦问题解决,我会通知您
  • @iamaphpdeveloper。它正在打印编辑值,并且 id 也是正确的。仍然没有进入 ($this->Category->save($this->request->data))。

标签: cakephp


【解决方案1】:
Try: $this->Category->id = $this->request->data['id'];    if($this->Category->save($this->request->data)){
           $this->Session->setFlash('The data has been edited successfully');
           return $this->redirect(array('action' => 'index'));
           //$this->redirect('index');
       }

【讨论】:

  • 谢谢大家!我找到了解决方案。我的模型似乎有一些错误。我上面的代码是对的。
【解决方案2】:

传递$id查看 -

$this->set(compact('id'));

然后将其设置为字段-

echo $this->Form->input('id',array('type'=>'hidden', 'value' => $id));

如果您有一个隐藏的 id 字段,那么为什么要显示另一个?如果不需要,请删除,否则正确分配值。希望它会起作用。

【讨论】:

  • 是的。我这样做了。我回应了身份证号码。这是正确的。我做了和你一样的事情。但它仍然没有执行 ($this->Category->save($this->request->data))。
  • 它没有保存或更新?
猜你喜欢
  • 1970-01-01
  • 2012-05-20
  • 1970-01-01
  • 2011-11-11
  • 2014-05-04
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多