有没有办法检查cake php中是否存在记录。我知道有一个函数。CakePHP 2
$this->Notes->id = $id;
if (!$this->Notes->exists())
{
throw new NotFoundException(__('Invalid Notes'));
}
但默认情况下,它使用列id
进行检查。假设它是note_id
,我如何使用自定义列进行检查。我的尝试是从这里参考
尝试#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])
如果发现其他false
,将返回true
hasAny在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版本中不可用