moluv00
moluv00

Reputation: 374

Using PHP Activerecord Finders in Classes

I'm trying to test if a record exists with the following code using PHP Activerecord:

class User extends ActiveRecord\Model {

    public function user_exists() {
        $user = User::find_by_email($this->email);
        if ( $user ) {
            return true;
        } else {
            return false;
        }
    }

}

$user = new User();
$user->email = "[email protected]";

if ( $user->user_exists() ) {
    echo "user exists";
} else {
    echo "no user";
}

For some reason, I'm getting a 'Call to undefined method: find_by_email' error. What am I doing wrong?

Upvotes: 0

Views: 149

Answers (1)

Koen.
Koen.

Reputation: 26999

Try the exists method:

User::exists(array('email' => $this->email));

Which is faster because it only checks for existence but does not fetch the data.

Or even better; use model validations:

http://www.phpactiverecord.org/projects/main/wiki/Validations#validates_uniqueness_of

Upvotes: 1

Related Questions