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.

I have searched google, but I can't find any topic about this. Well, a lot about php and mysql, but not about cakephp. I need a cake specific answer.

Thanks for your time :)

Answers


What you are looking for is 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
}

Need Your Help

How to add Project Reference in asp.net 5 application

nuget-package asp.net-core visual-studio-2015

As per my knowledge, Visual Studio 2015 has some update and we can't add DLLs in asp.net 5 application any more, if we need to add then we need to make NuGet package and then install it.

Eclipse started crashing

eclipse crash stack-overflow eclipse-pdt eclipse-3.6

I have been using eclipse for a couple of weeks now, and all of a sudden it has started crashing with this error: