Prabhakaran
Prabhakaran

Reputation: 4013

Yii framework where in clause

I am trying to fetch result with where in condition

$email = '[email protected]';                   // This works
$email = '[email protected],[email protected]';   // This fails

$users = User::model()->findAllByAttributes(array('email'=>array($email)));
print_r($users);

Where i am wrong.

Upvotes: 0

Views: 1058

Answers (3)

Kalpit
Kalpit

Reputation: 4936

you can try this way...

$email1="[email protected]";
$email2="[email protected]";
$users = User::model()->findAllByAttributes(array('email'=>array($email1,$email2)));
print_r($users);

if you want to check multiple emails then use CDbcriteria..

addInCondition() this is the best way...

for ex..

$email = '[email protected],[email protected]'; 
$emails = explode(',',$email);
$criteria->addInCondition('email',$emails);
$user=User::model()->findAll(array($criteria)));
print_r($users);

didn't try this code practically but hope it may work/help you...

Upvotes: 1

Tahir Yasin
Tahir Yasin

Reputation: 11699

Use addInCondition for such scenarios.

$emails = '[email protected],[email protected]';
$emailsArray = explode(',', $emails);
$criteria = new CDbCriteria();
$criteria->addInCondition("email", $emailsArray);
$users = User::model()->findAll($criteria);

Upvotes: 1

Martin Komara
Martin Komara

Reputation: 767

That depends on what you want to achieve. If you want to look up all users with matching addresses, you may use explode to convert comma separated list to array:

$emails = explode(',', $email);
$users = User::model()->findAllByAttributes(array('email'=>$emails));

Upvotes: 1

Related Questions