想要更新或 return false 而不是在 cakephp 中插入
Want to update or return false instead of insert in cakephp
我在 cakephp 中用 saveField
方法更新了一个字段。
示例:
$this->Attachment->id = $id;
$updateResult = $this->Attachment->saveField('dispatched', '1');
现在,如果 $id
的值不存在于数据库中,则 saveField
在数据库中插入一条新记录。但我想更新记录而不是新插入。如果数据库中不存在 $id
的值,那么我希望 saveField
中的 return false
我该怎么做?
只需将您的保存包装在一个条件中:
$this->Attachment->id = $id;
if(isset($id) {
//your save here
$updateResult = $this->Attachment->saveField('dispatched', '1');
}else{
return false;
}
评论后更新:
所以做一个计数并将其用作测试:
$settings = array(
'conditions'=>array(
'Atachment.id'=>$id;
)
);
$test = 0;
$test = $this->Attachment->find('count', $settings);
if($test >0 {
//your save here
} else {
//return false here
}
应该做。
你必须检查记录是否存在
if ($this->Attachment->findById($id)) {
$this->Attachment->id = $id;
return $this->Attachment->saveField('dispatched', '1');
} else {
return false;
}
我在 cakephp 中用 saveField
方法更新了一个字段。
示例:
$this->Attachment->id = $id;
$updateResult = $this->Attachment->saveField('dispatched', '1');
现在,如果 $id
的值不存在于数据库中,则 saveField
在数据库中插入一条新记录。但我想更新记录而不是新插入。如果数据库中不存在 $id
的值,那么我希望 saveField
我该怎么做?
只需将您的保存包装在一个条件中:
$this->Attachment->id = $id;
if(isset($id) {
//your save here
$updateResult = $this->Attachment->saveField('dispatched', '1');
}else{
return false;
}
评论后更新:
所以做一个计数并将其用作测试:
$settings = array(
'conditions'=>array(
'Atachment.id'=>$id;
)
);
$test = 0;
$test = $this->Attachment->find('count', $settings);
if($test >0 {
//your save here
} else {
//return false here
}
应该做。
你必须检查记录是否存在
if ($this->Attachment->findById($id)) {
$this->Attachment->id = $id;
return $this->Attachment->saveField('dispatched', '1');
} else {
return false;
}