简体   繁体   English

Cakephp检查记录是否存在

[英]Cakephp check if record exists

I am wondering, is there a function that allows me to check instantly if a record in the database exists? 我想知道,是否有一个函数可以让我立即检查数据库中的记录是否存在?

Right now I am using the following piece of code to detect if a record exists, but I can imagine there is a simpler/better way. 现在我正在使用以下代码来检测记录是否存在,但我可以想象有一种更简单/更好的方法。

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

Is there something like: 是否有类似的东西:

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

Okay, yes there is. 好的,是的。 It's called exists() , but I need the same, but with parameters, so I can add my own conditions to the check. 它被称为exists() ,但我需要相同的,但有参数,所以我可以添加自己的条件进行检查。

I have searched google, but I can't find any topic about this. 我搜索过谷歌,但我找不到任何关于此的话题。 Well, a lot about php and mysql, but not about cakephp. 好吧,很多关于php和mysql,但不是关于cakephp。 I need a cake specific answer. 我需要一个蛋糕特定的答案。

Thanks for your time :) 谢谢你的时间 :)

What you are looking for is Model::hasAny 您正在寻找的是Model :: hasAny

Usage: 用法:

$conditions = array(
    'User.id' => $this->Session->read('User.id'),
    'User.security_key' => $this->Session->read('User.key')
);
if ($this->User->hasAny($conditions)){
    //do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM