php 如何正确地将数据保存到数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11127210/
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-24 23:48:18  来源:igfitidea点击:

How do I properly save data to the database?

phpmysqlcakephpcakephp-2.0

提问by trante

I normally save new data to the database like this:

我通常像这样将新数据保存到数据库中:

$this->MyTable->set(array(
 'id' => $id,
 'code' => $temp_code,
 'status' => $status,
 'age' => $age,
 'location' => $location,
 'money' => $money
));

$this->MyTable->save();

If the ID already exists in the database I update its corresponding fields like this:

如果数据库中已经存在该 ID,我会更新其相应的字段,如下所示:

$this->Adtweet->id = $id;
$this->Adtweet->saveField('code', $temp_code);
$this->Adtweet->saveField('status', $status);

Is there a better or 'proper' way to do this?

有没有更好或“正确”的方法来做到这一点?

When I attempt to enter an ID that already exists and I use the setfunction, I get the following SQL integrity error:

当我尝试输入一个已经存在的 ID 并使用该set函数时,我收到以下 SQL 完整性错误:

(Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '150245925' for key 'PRIMARY')

(错误:SQLSTATE[23000]:违反完整性约束:1062 键“PRIMARY”的重复条目“150245925”)

How can I write a function that cleanly handles duplicate entries without returning an error?

如何编写一个函数来干净地处理重复条目而不返回错误?

回答by Julian Hollmann

If you want to save new data, just use Model::save():

如果要保存新数据,只需使用Model::save()

$data = array(
    'ModelName' => array(
        'foo' => $foo
    )
)

// prepare the model for adding a new entry
$this->ModelName->create();

// save the data
$this->ModelName->save($data);

If you want to update your data just use the same method without calling Model::create()

如果您想更新您的数据,只需使用相同的方法而无需调用 Model::create()

$data = array(
    'ModelName' => array(
        'id' => $id
        'foo' => $foo
    )
)

$this->ModelName->save($data);

See also: http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-save-array-data-null-boolean-validate-true-array-fieldlist-array

另见:http: //book.cakephp.org/2.0/en/models/ Saving-your-data.html#model-save-array-data-null-boolean-validate-true-array-fieldlist-array

Edit:

编辑:

I guess this is what you're looking for:

我想这就是你要找的:

$this->ModelName->id = $id;
if (!$this->ModelName->exists()) {
    $this->ModelName->create();
}

$this->ModelName->save($data);

回答by Sunil kumar

Posted data example

发布数据示例

Array
(
    [ModelName] => Array
        (
            [column1] => value
            [column2] => value
            [column3] => value
        )

)

Try this to add

试试这个添加

if ($this->request->is('post')) {
    $this->ModelName->create();
    $this->ModelName->save($this->request->data);
}

Try this to edit

试试这个来编辑

if ($this->request->is('post')) {
    $this->ModelName->id = 2;
    $this->ModelName->save($this->request->data);
 }