我想知道,是否有一个函数可以让我立即检查数据库中的记录是否存在?
现在我正在使用以下代码来检测记录是否存在,但我可以想象有一种更简单/更好的方法.
$conditions = array(
'conditions' => array(
'User.id' => $this->Session->read('User.id'),
'User.activation_key' => $this->Session->read('User.key')
)
);
$result = $this->User->find('first', $conditions);
if (isset($result['User'])){
//do something
}
Run Code Online (Sandbox Code Playgroud)
是否有类似的东西:
$conditions = array(
'conditions' => array(
'User.id' => $this->Session->read('User.id'),
'User.security_key' => $this->Session->read('User.key')
)
);
if ($this->User->exists($conditions)){
//do something
}
Run Code Online (Sandbox Code Playgroud)
好的,是的.它被称为exists(),但我需要相同,但有参数,所以我可以添加自己的条件进行检查.
我搜索过谷歌,但我找不到任何关于此的话题.好吧,很多关于php和mysql,但不是关于cakephp.我需要一个蛋糕特定的答案.
谢谢你的时间 :)
tig*_*ang 67
您正在寻找的是Model :: hasAny
用法:
$conditions = array(
'User.id' => $this->Session->read('User.id'),
'User.security_key' => $this->Session->read('User.key')
);
if ($this->User->hasAny($conditions)){
//do something
}
Run Code Online (Sandbox Code Playgroud)