Cameron
Cameron

Reputation: 28793

CakePHP is hasAny necesscary if I have a find already

I have the following function that finds a matching user and changes their status to active:

public function activate ($username = null, $key = null)
{
    if ($username == null || $key == null)
    {
        $this->redirect(array('action' => 'login'));
    }

    if ($this->User->hasAny(array(
            'User.username' => $username,
            'User.activation_key' => $key,
            'User.status' => 0,
        )))
    {
        $user = $this->User->findByUsername($username);
        $this->User->id = $user['User']['id'];
        $this->User->saveField('status', 1);
        $this->User->saveField('activation_key', md5(uniqid()));
        $this->Session->setFlash(__('Account activated successfully.', true), 'default', array('class' => 'success'));
    }
    else
    {
        $this->Session->setFlash(__('An error occurred.', true), 'default', array('class' => 'error'));
    }

    $this->redirect(array('action' => 'login'));
}

But is the if statement: if ($this->User->hasAny(array( 'User.username' => $username, 'User.activation_key' => $key, 'User.status' => 0, ))) even necessary as I still have to find the user via their email address (which is obviously unique)

Upvotes: 0

Views: 3577

Answers (2)

Devendra Soni
Devendra Soni

Reputation: 391

It is not necessary. When you need some extra conditions to be fulfilled you can put them into your find condition .

Upvotes: 0

Anh Pham
Anh Pham

Reputation: 5481

obviously not.

And this is the first time I know there's hasAny() in cake :))

Upvotes: 1

Related Questions