CakePHP - 为什么Model :: save cause()是INSERT而不是UPDATE? [英] CakePHP - Why does Model::save cause() an INSERT instead of an UPDATE?

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

问题描述

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

  $ data = array $ b'KnowledgeBase'=> array(
'kb_title'=> $ this-> data ['KnowledgeBase'] ['kb_title'],
'kb_content'=> $ this- > data ['KnowledgeBase'] ['kb_content']
'kb_last_update'=> date(Ymd G:i:s),
'kb_segment'=> $ this-> data ['KnowledgeBase'] ['kb_segment']
));
$ this-> KnowledgeBase-> id_kb = $ this-> data ['KnowledgeBase'] ['id_kb'];
$ this->知识库 - > save($ data);

假设我有post表单是true,当我执行程序
我有一些错误像这样:

 数据库错误

错误:SQLSTATE [23000]:[Microsoft] Native Client 10.0]
[SQL Server]违反PRIMARY KEY约束PK_cnaf_kb。
无法在对象'dbo.cnaf_kb'中插入重复键。

SQL查询: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)

为什么函数使用insert query?不更新?



注意:我没有使用形式助手的post到控制器,我使用Cakephp 2.3.8版本和sql server 2008数据库



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

解决方案

您不提供主键值,这就是为什么。



无论您的主键是什么命名( Model :: $ primaryKey ),on你必须使用 id 属性( Model :: $ id )。

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



内部模型将此映射到相应的主键字段。



但您可以使用实际的主键名称:

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

btw不确定你为什么(重新)建立数据数组,但如果它是为了确保只保存特定的字段,那么你可以使用 fieldList 选项

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

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

);

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


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 ?

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.

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']

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆