ActiveRecord Content

I have two version of a member method of User extended from CActiveRecord. On only difference is $email as method parameter. Here are the columns of User table:


/**

 * This is the model class for table "user".

 *

 * The followings are the available columns in table 'user':

 * @property integer $id

 * @property string $password

 * @property string $email

 */

I can do the following in controller:


            $potentialUser = new User();

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

            $potentialUser->password = '123456';

But why cannot do this: (emailExist() is a method of User)


       public function emailExist(){

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

        }

This version works, but I have to pass $email every time. Isn’t $email should be a member of User automatically?


        public function emailExist($email){

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

        }

Why accessing member properties from within the AR class method is not allowed?

You need to specify the object. In this case, it is


$this

. Then:


public function emailExist(){

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

}

Thanks!