How to use findByAttributes() in ActiveRecord

This is the definition of findByAttributes()

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findByAttributes-detail

But sadly, I cannot understand it.

public CActiveRecord findByAttributes(array $attributes, mixed $condition=’’, array $params=array ( ))

I extended a User class from CActiveRecord using Gii. Then using the following function to see if the email is in DB already.


        public function emailExist(){

            return $this->findByAttributes('email', array($email));

        }

before calling emailExist(). I executed the following code:


            $potentialUser = new User();

            $potentialUser->email = 'test@hotmail.com';

            $potentialUser->password = '123456';

            if($potentialUser->emailExist()) {

                echo 'user exist';

            }

But got the following error:

Here is the three columns in User table:

  • @property integer $id

  • @property string $password

  • @property string $email

What is the correct way to pass parameters to findByAttributes()? I hope they could insert one or two examples to the class guide, otherwise I am not capable to know how to use the functions even after reading the class reference.




        public function emailExist(){

            return $this->findByAttributes(array('email'=>$this->email)) !== null;

        }



You can also use exists():




        public function emailExist(){

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

        }



Should work faster.

Thanks a lot! They all work like charm!

But I encounter another confusing issue: If I replace "$this->email" with "$email", then got an error "email is not defined"

Why I have to explicitly use "$this->" before a member property in a member method of AR class?

If you came from another language (e.g. C++) then you’ll probably find this a little strange :)

But inside a PHP method $email is just a local variable (which is undefined). You must always prepend class variables and methods with $this to call them from inside a class.




public function foo()

{

    $foo = 'bar';

    $this->foo = $foo; // $this->foo now == 'bar'

}



:rolleyes:

haha, I did learn a little C++ back in college. Thanks again!

Very nice explanation from andy_s.

Also, if you want to check if the email is unique on the database, you can use the CUniqueValidator.

How did you learn to pass the parameter? I couldn’t find available info to help.


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

Now, I tried the following way to pass multiple conditions


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



It’s not working. Can you show me how to specify multiple conditions?

Thanks!

Should be:




$this->exists('email = :email AND/OR password =:password', array(':email'=>$this->email, ':password'=>$this->password));



It’s just a condition inserted after sql’s “WHERE” keyword.

perfect. Thank you!

oh, exist ~~