public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('entity_category_id, entity_id', 'required'),
array('preferred','boolean'),
// unique with condition
array('email','unique',
'criteria'=>array(
'condition' => 'gmail= :gmail OR email= :email',
'params' => array(':email' => $this->email, ':gmail' => $this->email)
),
),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, entity_category_id, entity_id', 'safe', 'on'=>'search'),
);
}
Above unique query run WHERE clause as (gmail = 'abc' OR email= 'abc') AND (email = 'abc) but I don't want and condition, i want only (gmail = 'abc' OR email= 'abc') in where clause
It's normal that criteria is added to the original condition. From the Yii API:
criteria: additional query criteria. Either an array or CDbCriteria. This will be combined with the condition that checks if the attribute value exists in the corresponding table column
I think the easiest way to resolve your problem is to create your own validator in your model class:
public function EmailValidator($attribute,$params)
{
$criteria = new CDbCriteria;
$criteria->condition = 'gmail= :gmail OR email= :email';
$criteria->params = array(':email' => $this->email, ':gmail' => $this->email);
$result = $this->findAll($criteria);
if(!empty($result)) {
$this->addError($attribute, 'Email already exists!');
}
return;
}
And call it in your rules
method:
public function rules()
{
return array(
//Other rules
array('email', 'EmailValidator'),
);
}