簡體   English   中英

Cakephp:更新記錄而不引用主鍵

[英]Cakephp: Update records without refering the primary key

我正在嘗試更新與其線程ID相關的記錄數。 這是一個問題,我的id_thread字段不是主鍵。 但據我所知,cakephp僅支持更新與主要ID相關的內容。

用過的代碼

$this->contact->id_thread = $id;        
$this->Contact->saveField('is_read','y');

但這對我沒有用。 有任何想法嗎 ?

我的DB結構

id   |id_thread |  id_sender| is_read | status
_______________________________________________
1    | 2        |   2       | n       | 1
2    | 2        |   1       | n       | 1  

saveField僅用於處理當前加載的記錄。 要更新同一模型的多個記錄,請使用updateAll

用法:

Model :: updateAll(array $ fields,array $ conditions)

在一次通話中更新許多記錄。 要更新的記錄由$ conditions數組標識,要更新的字段及其值由$ fields數組標識。

資源

在你的情況下,它看起來像這樣:

$this->Contact->updateAll(
    array( 'Contact.is_read' => 'y' ),   //fields to update
    array( 'Contact.id_thread' => $id )  //condition
);

您的id_thred字段似乎不是唯一的。 因此,即使您進行SQL查詢來更新數據,它也會更新這兩行。 這是你期待的嗎?

我的建議是調用條件為id_thread的find方法並獲取主鍵id並使用saveField方法更新值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM