php - Cakephp 检查记录是否存在

标签 php cakephp

我想知道,有没有一个功能可以让我立即检查数据库中的记录是否存在?

现在我正在使用以下代码来检测记录是否存在,但我可以想象有更简单/更好的方法。

$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
}

是否有类似的东西:

$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
}

好的,是的。它称为 exists(),但我需要相同的参数,但带有参数,因此我可以将自己的条件添加到检查中。

我已经搜索过谷歌,但我找不到任何关于这个的话题。嗯,很多关于 php 和 mysql 的,但不是关于 cakephp 的。我需要一个特定于蛋糕的答案。

感谢您的宝贵时间:)

最佳答案

你要找的是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
}

关于php - Cakephp 检查记录是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11996823/

相关文章:

php - 如何强制 mySQL 数据库存储这个特殊字符 : ╠?

php - 从另一个模型cakephp 3.5获取数据

cakephp - 如何更新 CakePHP 中的现有记录?

php - 获取日期范围内的用户生日

cakephp - 使用 CakePHP 1.3,您如何处理实时 HTTPS URL 和开发人员的普通 HTTP URL?

deployment - CakePHP 应用程序部署

php - 上传到 mySQL 并使用 SimpleImage PHP?

php - 无法让 MailChimp 使用带有 Curl 的 API 保存我的数据

php - Laravel Join 中的 where 条件不起作用

php - Bootstrap 导航栏上的帐户信息