nettrinity
(Thanhvn Info)
April 16, 2011, 9:30pm
1
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:
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.
andy_s
(Arekandrei)
April 16, 2011, 10:10pm
2
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.
nettrinity
(Thanhvn Info)
April 16, 2011, 10:21pm
3
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?
andy_s
(Arekandrei)
April 16, 2011, 10:26pm
4
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'
}
nettrinity
(Thanhvn Info)
April 16, 2011, 11:08pm
5
haha, I did learn a little C++ back in college. Thanks again!
mentel
(Yii)
April 16, 2011, 11:21pm
6
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.
nettrinity
(Thanhvn Info)
April 16, 2011, 11:28pm
7
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!
andy_s
(Arekandrei)
April 17, 2011, 7:20am
8
nettrinity:
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.