CakePHP - 为什么 Model::save cause() 是 INSERT 而不是 UPDATE?

CakePHP - Why does Model::save cause() an INSERT instead of an UPDATE?(CakePHP - 为什么 Model::save cause() 是 INSERT 而不是 UPDATE?)
本文介绍了CakePHP - 为什么 Model::save cause() 是 INSERT 而不是 UPDATE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想以 CAKEPHP 的方式更新数据库这是我的控制器

I want to update database in CAKEPHP's Way this is my controller

$data = array(
'KnowledgeBase' => array(
'kb_title' => $this->data['KnowledgeBase']['kb_title'],
'kb_content' => $this->data['KnowledgeBase']['kb_content']
'kb_last_update' => date("Y-m-d G:i:s"),
'kb_segment' => $this->data['KnowledgeBase']['kb_segment']
));
$this->KnowledgeBase->id_kb = $this->data['KnowledgeBase']['id_kb'];
$this->KnowledgeBase->save($data);

假设我的帖子形式是真的,当我执行程序时我有一些这样的错误:

assume I have post form is true, when I execute the program I have some error like this :

Database Error

 Error: SQLSTATE[23000]: [Microsoft][SQL Server Native Client 10.0]
[SQL Server]Violation of PRIMARY KEY constraint 'PK_cnaf_kb'.
 Cannot insert duplicate key in object 'dbo.cnaf_kb'.

SQL Query: INSERT INTO [cnaf_kb] ([kb_judul], [kb_segment], [kb_isi], [id_kb], [kb_last_update], [kb_status]) VALUES (N'HARRIS TEST 4 ', N'4', N'<p>TESSSSSSSSSSSSSSSSSSSSSS</p> ', 73, 
'2013-10-04 16:57:00', 1)

为什么函数使用插入查询?不更新?

why the function use the insert query? not update ?

注意:我没有使用表单助手来发布到控制器,我使用 Cakephp 2.3.8 版本和 sql server 2008 作为数据库

note : im not using form helper for post to controller, and I use Cakephp 2.3.8 version and sql server 2008 for database

对不起,我的英语不好,我希望有人能帮助我:(((

Im sorry for my bad english, I hope someone can help me :(((

推荐答案

您没有提供主键值,这就是原因.

You do not supply a primary key value, that's why.

不管你的主键叫什么(Model::$primaryKey),在模型对象上你必须使用 id 属性(Model::$id) 如果要设置主键值.

No matter what your primary key is named (Model::$primaryKey), on the model object you have to use the id property (Model::$id) if you want to set the primary key value.

$this->KnowledgeBase->id = $this->data['KnowledgeBase']['id_kb'];

模型在内部将其映射到适当的主键字段.

Internally the model maps this to the appropriate primary key field.

在数据中,您将使用实际的主键名称:

In the data however you'd use the actual primary key name:

'id_kb' => $this->data['KnowledgeBase']['id_kb']

顺便说一句,我不确定您为什么要(重新)构建 data 数组,但是如果要确保仅保存特定字段,那么您可以使用 fieldList 选项 代替:

btw, I'm not sure why you are (re)building the data array, but if it's to make sure that only specific fields are saved, then you could use the fieldList option instead:

$this->data['KnowledgeBase']['kb_last_update'] = date('Y-m-d G:i:s');

$options = array(
    'fieldList' => array(
        'kb_title',
        'kb_content',
        'kb_last_update',
        'kb_segment'
    )
);

$this->KnowledgeBase->save($this->data, $options);

这篇关于CakePHP - 为什么 Model::save cause() 是 INSERT 而不是 UPDATE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)