6

2 つのテーブルがtable1あり、table2それぞれemailに別の列と一緒に名前が付けられた列があります。私が欲しいのは、email両方の列のフィールドで一意性を探すバリデーターです。SAMEテーブルの複数の列をチェックする拡張機能を見つけました。複数の列で機能するように拡張するにはどうすればよいですか?

4

2 に答える 2

7

classNameプロパティを使用して、他のクラスを指定できます。

ドキュメンテーション: the ActiveRecord class name that should be used to look for the attribute value being validated. Defaults to null, meaning using the class of the object currently being validated. You may use path alias to reference a class name here.

2つのモデルでcommon_attrという属性を設定しましょう。

class Model1 extends CActiveRecord{
    public function rules(){
        array('common_attr', 'unique', 'className'=> 'Model1'),
        array('common_attr', 'unique', 'className'=> 'Model2'),
    }
}

class Model2 extends CActiveRecord{
    public function rules(){
        array('common_attr', 'unique', 'className'=> 'Model1'),
        array('common_attr', 'unique', 'className'=> 'Model2'),
    }
}

またcombined key、複数のテーブルからの検証を確認するには、CUniqueValidatorのcriteriaプロパティを使用できます。 拡張機能は必要ありません。

ドキュメンテーション:criteria property public array $criteria; additional query criteria. This will be combined with the condition that checks if the attribute value exists in the corresponding table column. This array will be used to instantiate a CDbCriteria object.

class Model1 extends CActiveRecord{

    public function rules(){
        array('common_attr', 'unique', 'caseSensitive'=>false,
                   'criteria'=>array(
            'join'=>'LEFT JOIN model2 ON model2.common_attr=model1.common_attr',
                            'condition'=>'model2.common_attr=model1.common_attr',
        )),
    }
}
于 2012-06-19T03:52:21.203 に答える