gpt4 book ai didi

php - Cakephp:更新记录而不引用主键

转载 作者:行者123 更新时间:2023-12-04 22:19:46 25 4
gpt4 key购买 nike

我正在尝试根据线程 ID 更新记录数。这是一个问题,我的 id_thread 字段不是主键。但据我所知,cakephp 只支持更新与 primary id 相关的内容。

使用代码

$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

最佳答案

saveField 仅用于处理当前加载的记录。要更新同一模型的多个记录,请使用 updateAll

用法:

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

Updates many records in a single call. Records to be updated are identified by the $conditions array, and fields to be updated, along with their values, are identified by the $fields array.

source

在您的情况下,它看起来像这样:

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

关于php - Cakephp:更新记录而不引用主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10509792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com