mysql - 在 CakePHP 3 中编辑主键

标签 mysql cakephp primary-key cakephp-3.0

我想编辑 CakePHP 3 中表 Watch 中名为 WTEL 的主键。

主键字段默认在 View 中隐藏,因此我使用此代码行使其显示在文件 src/Template/Watch/edit.ctp (由脚手架生成)中:

echo $this->Form->input('WTEL', array('type' => 'text'));

字段WTEL出现,但当我修改WTEL输入并单击提交按钮时,WTEL值未修改。

但是,此代码行适用于 add 页面,用于添加 WTEL 值。

最佳答案

澄清一下,您希望能够更新 edit.ctp View 中现有行的主键(使用 Watch Controller 中的默认烘焙编辑功能)?

最简单的方法是创建一个“复制”功能,大致如下:

public function copy($id = null){

    //get the old Watch to be copied
    $oldWatch = $this->Watch->get($id);

    $watchTable = TableRegistry::get('Watchs');
    $watch= $watchsTable->newEntity();

    //Copy all existing values into new row, e.g.
    $watch->description = $oldwatch->description;

    if ($watchsTable->save($watch)) {
        // The $watch entity contains the id now, so you are returned 
           to the edit page for the newly created 'watch'
        $id = $watch->id;
    }

    return $this->redirect(['action' => 'edit', $id]);
}

如果您想创建自定义主键,可以尝试添加以下内容:

$watch->id = $this->data['Watch']['id'];

在上面评论的“复制所有现有值”部分中。

这能回答你的问题吗?

关于mysql - 在 CakePHP 3 中编辑主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34637292/

相关文章:

php - 使用 cakephp 按日期分组

mysql - 如何在MySQL中进行组内聚合查询?

mysql - InnoDB 行大小在表增长时呈指数变化?

数据库:主键,集群或非集群

MySQL 按字段加字段排序

mysql - 无法设置关系 phpmyadmin

java - "Accesing data with MySQL"教程之后的异常 - SpringBoot JPA Hibernate

python - Webiopi (Raspberry pi) 与 sql 或 sqlite

mysql - 数据库错误 PDOException 错误 : SQLSTATE[HY000] [1045]

Mysql - 如何设置自增从零开始