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'),
);
}
以上唯一查询将WHERE子句运行为(gmail =' abc' OR email =' abc')AND(email =' abc) 但我不想要和条件,我只想要(gmail =' abc'或电子邮件=' abc')在where子句中
答案 0 :(得分:1)
将标准添加到原始条件是正常的。来自Yii API:
条件:其他查询条件。数组或CDbCriteria。这将与检查相应表列
中是否存在属性值的条件相结合
我认为解决问题的最简单方法是在模型类中创建自己的验证器:
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;
}
并使用rules
方法调用它:
public function rules()
{
return array(
//Other rules
array('email', 'EmailValidator'),
);
}