【问题标题】:Modify a row with CakePHP用 CakePHP 修改一行
【发布时间】:2014-01-13 13:13:06
【问题描述】:

我正在使用 CakePHP 显示 MySQL 数据库表的前端 GUI。我使用bake 自动生成屏幕,我目前有一个功能齐全的应用程序,每行都有查看、编辑和删除按钮。我想为每行添加一个按钮,称为Accept,它应该在 SQL 行上设置IsAccepted = 1

我设法为每行添加了一个Accept 按钮,如下所示:

 echo $this->Html->link(__('Accept'), array('action' => 'accept', $product['Product']['ID']))

但是ProductController.php中的代码不起作用:

 public function accept($id = null){
     ...
     $this->Product->IsAccepted = 1; // does not work, silently fails
 }

我做错了什么?如何使用逐行按钮正确编辑行?

【问题讨论】:

  • 这不是任何当前或以前的 CakePHP 版本的工作方式。检查the cookbook,请务必提及您的确切 CakePHP 版本!
  • 还有,你调用的action是“approve”,为什么函数叫“accept”?
  • 错字!对不起!我当前的 CakePHP 版本是 2.4.4。
  • @ndm - 使用上面显示的代码,该按钮每行可见!我只是复制了用于查看和编辑的系统(它们都有相同的代码行,每个按钮 1 行)......我只是无法弄清楚控制器中的代码......

标签: php mysql cakephp cakephp-2.4


【解决方案1】:
public function accept($id = null){
     $this->Product->save(array('id' => $id, 'is_accepted' => 1));
}

【讨论】:

  • 不错的尝试,但此代码出于某种原因执行了“INSERT”...然后以Duplicate entry '1' for key 'PRIMARY' 崩溃!
  • book.cakephp.org/2.0/en/models/… - "创建或更新由模型的 id 字段控制。如果设置了 $Model->id,则更新具有此主键的记录。否则创建新记录"。请尝试文档中描述的选项
【解决方案2】:
// assuming cake 2.1+
public function accept($id = null){
    if($this->Product->exists($id)) {
        $this->Product->saveField('is_accepted', 1);
        // success.. 
    }

    // else throw not found exception... 
}

【讨论】:

    【解决方案3】:

    感谢cornelb,我找到了答案!这是我用来修改一行的最终代码,带有每行按钮。

    • 按下每行按钮时修改行(就像 POST/AJAX 按钮一样)
    • 显示“已接受!”的快速消息显示是否保存成功
    • 重定向回列表页面(似乎永远不会离开列表)

    这是ProductController.php(或您拥有的任何控制器类)中的代码:

    public function accept($id = null) {
        if ($this->Product->exists($id)) {
    
            // save the row
            // you absolutely need to fill the 'id' slot, even if its not your primary key!!
            // this ensures that the row is EDITED, and not INSERTED!
            if($this->Product->save(array('id' => $id, 'ID' => $id, 'IsApproved' => 1, 'ApprovedDate' => date('Y-m-d H:i:s', time())))){
    
                    // show a "flash" message
                    // (not Adobe Flash, just a message that shows on top of the list)
                    $this->Session->setFlash(__('The product has been accepted!'));
    
                    // this action does not have a view so no need to render
                    $this->autoRender = false;
    
                    // redirect to index view
                    return $this->redirect(array('action' => 'index'));
            }
        }
    }
    

    【讨论】:

      【解决方案4】:
          **Try this.....**
      <?php
      public function accept($id = null) {
          $this->autoRender = false; // if action has not view.
          if ($this->Product->exists($id)) {
      
              $this->Product->id = $id;
              if ($this->Product->save(array('is_accepted' => 1))) {
                  $this->Session->setFlash(__('The product has been accepted!'));
                  return $this->redirect(array('action' => 'index'));
              }
          }
      }
      ?>
      

      【讨论】:

        【解决方案5】:

        只需从接受函数运行 updateAll 查询,如下所示:

        public function accept($id = null){
        
          if(!empty($id)){
           $this->Product->updateAll(
            array('Product.is_accepted' => 1),
            array('Product.id' => $id)
           );
          }
        
        }
        

        希望对您有所帮助...

        供参考:http://book.cakephp.org/2.0/en/models/saving-your-data.html

        【讨论】:

          猜你喜欢
          • 2012-01-22
          • 2012-08-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-16
          相关资源
          最近更新 更多