我想编辑 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/