dss89001 2014-06-17 10:56
浏览 32
已采纳

Yii:如何改变独特的标准?

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

  • 写回答

1条回答 默认 最新

  • doujiong2533 2014-06-17 11:35
    关注

    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'),
        );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置