【问题标题】:Yii unique validation on two columns of different tables ie. composite unique validationYii 对不同表的两列的唯一验证,即。复合唯一验证
【发布时间】:2012-06-20 17:06:44
【问题描述】:

我有两个表table1table2,每个表都有一个名为email 的列以及不同的其他列。我想要的是一个验证器,它在两列的email 字段中查找唯一性。我找到了一个extension,它检查了 SAME 表的多个列。如何扩展它以使其适用于多列?

【问题讨论】:

    标签: php validation yii composite-key unique-constraint


    【解决方案1】:

    您可以使用 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.

    让我们在两个模型中有一个名为 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 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',
            )),
        }
    }
    

    【讨论】:

      【解决方案2】:

      Yii2

      短:

      ['common_attr', 'unique'],
      ['common_attr', 'unique', 'targetClass' => AnotherClass::class],
      

      详细:

      class One extends \yii\db\ActiveRecord
      {
          public function rules()
          {
              return [
                  ['common_attr', 'unique'],
                  ['common_attr', 'unique', 'targetClass' => Two::class],
              ];
          }
      }
      
      class Two extends \yii\db\ActiveRecord
      {
          public function rules()
          {
              return [
                  ['common_attr', 'unique'],
                  ['common_attr', 'unique', 'targetClass' => One::class],
              ];
          }
      }
      

      此外,您还可以在 targetAttribute 中使用多个唯一键:

      [
          'common_attr', 
          'unique', 
          'targetClass' => One::class, 
          'targetAttribute' => ['common_attr', 'one_more_attr']
      ]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-10
        • 2020-04-30
        • 1970-01-01
        • 1970-01-01
        • 2016-03-29
        • 2018-10-25
        • 2021-09-11
        相关资源
        最近更新 更多