Cakephp:在不引用主键的情况下更新记录
我正在尝试更新与其线程Id相关的记录数。这里有一个问题,我的Id\u线程字段不是主键。但据我所知,cakephp只支持更新与主id相关的内容 使用的代码Cakephp:在不引用主键的情况下更新记录,php,cakephp,Php,Cakephp,我正在尝试更新与其线程Id相关的记录数。这里有一个问题,我的Id\u线程字段不是主键。但据我所知,cakephp只支持更新与主id相关的内容 使用的代码 $this->contact->id_thread = $id; $this->Contact->saveField('is_read','y'); id |id_thread | id_sender| is_read | status ______________________________
$this->contact->id_thread = $id;
$this->Contact->saveField('is_read','y');
id |id_thread | id_sender| is_read | status
_______________________________________________
1 | 2 | 2 | n | 1
2 | 2 | 1 | n | 1
但这对我来说是行不通的。有什么想法吗
我的数据库结构
$this->contact->id_thread = $id;
$this->Contact->saveField('is_read','y');
id |id_thread | id_sender| is_read | status
_______________________________________________
1 | 2 | 2 | n | 1
2 | 2 | 1 | n | 1
您的
id\u thred
字段似乎不是唯一的。因此,即使您进行SQL查询来更新数据,它也会更新这两行。这是你期待的吗
我的建议是使用条件调用find方法
id\u thread
,获取主键id并使用saveField
方法更新值saveField仅用于当前加载的记录。要更新同一型号的多条记录,请使用updateAll
用法:
Model::updateAll(数组$fields,数组$conditions)
在一次呼叫中更新多个记录。要更新的记录由$conditions数组标识,要更新的字段及其值由$fields数组标识
在您的情况下,它将如下所示:
$this->Contact->updateAll(
array( 'Contact.is_read' => 'y' ), //fields to update
array( 'Contact.id_thread' => $id ) //condition
);
不,正是我想要的,更新多行