久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

CakePHP - 為什么 Model::save cause() 是 INSERT 而不是

CakePHP - Why does Model::save cause() an INSERT instead of an UPDATE?(CakePHP - 為什么 Model::save cause() 是 INSERT 而不是 UPDATE?)
本文介紹了CakePHP - 為什么 Model::save cause() 是 INSERT 而不是 UPDATE?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我想以 CAKEPHP 的方式更新數(shù)據(jù)庫這是我的控制器

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);

假設(shè)我的帖子形式是真的,當(dāng)我執(zhí)行程序時我有一些這樣的錯誤:

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)

為什么函數(shù)使用插入查詢?不更新?

why the function use the insert query? not update ?

注意:我沒有使用表單助手來發(fā)布到控制器,我使用 Cakephp 2.3.8 版本和 sql server 2008 作為數(shù)據(jù)庫

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) 如果要設(shè)置主鍵值.

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

模型在內(nèi)部將其映射到適當(dāng)?shù)闹麈I字段.

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

在數(shù)據(jù)中,您將使用實際的主鍵名稱:

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

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

順便說一句,我不確定您為什么要(重新)構(gòu)建 data 數(shù)組,但是如果要確保僅保存特定字段,那么您可以使用 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);

這篇關(guān)于CakePHP - 為什么 Model::save cause() 是 INSERT 而不是 UPDATE?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應(yīng)用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內(nèi)存大小為 134217728 字節(jié)已用盡(嘗試分配 87 字節(jié)))
主站蜘蛛池模板: 成年人黄色一级片 | 欧美成人精品一区二区男人看 | 精品久久久久久久久久久久久 | 国产在线二区 | 激情在线视频网站 | 国产精品视频网 | 久久国| 九九精品视频在线 | 国产探花在线精品一区二区 | 日韩欧美在线观看视频 | 国产一区二区三区高清 | 久久国产成人精品国产成人亚洲 | 久久黄色精品视频 | 成人欧美一区二区三区黑人孕妇 | 久久国产综合 | 久久久综合| 一级在线视频 | 中文字幕在线观看一区二区 | 最新午夜综合福利视频 | 久久久久久免费毛片精品 | 久草新在线| 中文字幕一区二区三区四区五区 | 久久人体视频 | 亚洲欧美在线视频 | 亚洲一区二区三区福利 | 国产一级大片 | 伊人在线 | 久久99精品国产99久久6男男 | 亚洲人在线播放 | 国产激情福利 | 国产精品久久精品 | 久久精品99 | www成人免费视频 | 亚洲 欧美 日韩在线 | 国产在线a视频 | 久久久资源 | 日本天堂视频在线观看 | 一区二区免费 | 天堂av在线影院 | 国产成人综合网 | 精品国产乱码久久久久久丨区2区 |