cake php 检查自定义列中是否存在记录
cake php check if record exist with custom column
有没有办法检查蛋糕中是否存在记录php。我知道有一个函数 .CakePHP 2
$this->Notes->id = $id;
if (!$this->Notes->exists())
{
throw new NotFoundException(__('Invalid Notes'));
}
但默认情况下它检查列 id
我如何检查自定义列假设它是 note_id
。我的尝试是 refer from here
尝试#1
if (!$this->Noteshistory->exists(['note_id'=>$id]))
{
throw new NotFoundException(__("Invalid Note "));
}
也试过设置note_id
$this->Noteshistory->note_id = $id;
if (!$this->Noteshistory->exists(['note_id'=>$id]))
{
throw new NotFoundException(__("Invalid Note "));
}
但运气不好。
hasAny 是解决方案 -
$this->Noteshistory->hasAny(['note_id'=>$id])
将 return true
if found else false
hasAny is not available in version 3.x
你可以使用hasAny()
:-https://api.cakephp.org/2.6/class-Model.html#_hasAny
$conditions = array(
'note_id'=>$id
);
if ($this->Noteshistory->hasAny($conditions)){
//do something
}
注意:- 在 3.x 版本
中不可用
有没有办法检查蛋糕中是否存在记录php。我知道有一个函数 .CakePHP 2
$this->Notes->id = $id;
if (!$this->Notes->exists())
{
throw new NotFoundException(__('Invalid Notes'));
}
但默认情况下它检查列 id
我如何检查自定义列假设它是 note_id
。我的尝试是 refer from here
尝试#1
if (!$this->Noteshistory->exists(['note_id'=>$id]))
{
throw new NotFoundException(__("Invalid Note "));
}
也试过设置note_id
$this->Noteshistory->note_id = $id;
if (!$this->Noteshistory->exists(['note_id'=>$id]))
{
throw new NotFoundException(__("Invalid Note "));
}
但运气不好。
hasAny 是解决方案 -
$this->Noteshistory->hasAny(['note_id'=>$id])
将 return true
if found else false
hasAny is not available in version 3.x
你可以使用hasAny()
:-https://api.cakephp.org/2.6/class-Model.html#_hasAny
$conditions = array(
'note_id'=>$id
);
if ($this->Noteshistory->hasAny($conditions)){
//do something
}
注意:- 在 3.x 版本
中不可用