cake php检查自定义列是否存在记录

Man*_*mar 5 php cakephp

有没有办法检查蛋糕php中是否存在记录.我知道有一个功能.CakePHP 2

$this->Notes->id = $id;
if (!$this->Notes->exists())
{
    throw new NotFoundException(__('Invalid Notes'));
}
Run Code Online (Sandbox Code Playgroud)

但默认情况下,它会检查列id如何检查自定义列,假设它是note_id.我的attemts是从这里引用的

尝试#1

if (!$this->Noteshistory->exists(['note_id'=>$id]))
{
    throw new NotFoundException(__("Invalid Note "));
}
Run Code Online (Sandbox Code Playgroud)

还尝试设置note_id

$this->Noteshistory->note_id = $id;
if (!$this->Noteshistory->exists(['note_id'=>$id]))
    {
        throw new NotFoundException(__("Invalid Note "));
    }
Run Code Online (Sandbox Code Playgroud)

但没有运气.

Sou*_*ose 12

hasAny是解决方案 -

$this->Noteshistory->hasAny(['note_id'=>$id])
Run Code Online (Sandbox Code Playgroud)

true如果找到别的话会返回false

hasAny3.x版本中不可用

  • 我的意思更多:不建议/习惯使用未来版本中不存在的功能 - 它使得任何未来的升级都变得更难.像hasAny这样的方法并没有真正帮助,因为它们只是隐藏它是一个计数(我个人认为我从未使用过hasAny),因此被删除了.没错,只是一个观察+推荐. (2认同)