2012-06-18 27 views
5

Tôi có hai bảng table1table2 mỗi cột có tên là email cùng với các cột khác nhau. Những gì tôi muốn là một trình xác thực tìm kiếm tính duy nhất trong trường email của cả hai cột. Tôi đã tìm thấy một extension kiểm tra nhiều cột của SAME bảng. Làm thế nào tôi có thể mở rộng nó để nó hoạt động cho nhiều cột?Xác thực duy nhất Yii trên hai cột của các bảng khác nhau, ví dụ: xác thực duy nhất tổng hợp

Trả lời

7

Bạn có thể sử dụng tài sản className để xác định cho các lớp học khác ..

Tài liệu: 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.

Cho phép có một thuộc tính gọi là common_attr trong hai mô hình:

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'), 
    } 
} 

Và để kiểm tra combined key xác thực từ nhiều bảng, bạn có thể sử dụng thuộc tính tiêu chí của CUniqueValidator ..Không cần bất kỳ phần mở rộng

Tài liệu: 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', 
     )), 
    } 
} 
+0

Phương pháp đầu tiên hoạt động giống như một nét duyên dáng. Cảm ơn :) – dInGd0nG

Các vấn đề liên quan