php cakephp 更新字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5202333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:36:40  来源:igfitidea点击:

cakephp Update field

phpcakephp

提问by aWebDeveloper

How do i force cake to update a field and not insert a new record.

我如何强制蛋糕更新字段而不是插入新记录。

It should fail if the id does not exist in the db

如果 id 不存在于数据库中,它应该会失败

I found to force a insertion i can do 'updated' => falseso if i do 'updated' => truewill it work

我发现强制插入我可以这样做'updated' => false,如果我这样做'updated' => true会起作用吗

回答by JohnP

If you want to just update a field, use the saveFieldmethod of the model

如果你只想更新一个字段,使用saveField模型的方法

$this->Order->id    = $id;
$this->Order->saveField('status', 'VOID');

Reference : http://book.cakephp.org/2.0/en/models/saving-your-data.html

参考:http: //book.cakephp.org/2.0/en/models/saving-your-data.html

回答by Orbit

//Create: id isn't set or is null
$this->Recipe->create();
$this->Recipe->save($this->data);

//Update: id is set to a numerical value 


$this->Recipe->id = 2;
$this->Recipe->save($this->data);

see http://book.cakephp.org/2.0/en/models/saving-your-data.html

http://book.cakephp.org/2.0/en/models/saving-your-data.html

回答by AD7six

Use Model::exists

使用模型::存在

To ensure that the record does exist, use Model::Exists. For example:

要确保记录确实存在,请使用Model::Exists。例如:

function updateFoo($id, $value) {
    if (!$this->exists($id) {
        return false;
    }

    $this->id = $id;
    return $this->saveField('foo', $value);
}

This would act like so:

这会像这样:

$false = $model->updateFoo(false, $value);
$false = $model->updateFoo(0, $value);
$false = $model->updateFoo('doesnotexist', $value);
$true = $model->updateFoo('doesexist', $value);