yii: how to make a unique rule for two attributes

后端 未结 8 486
一生所求
一生所求 2020-12-29 02:26

I have a table like this: (id, name, version, text). (name, version) is unique key, how can i make a rule to validate this.

相关标签:
8条回答
  • 2020-12-29 03:12

    Yii1 :

    http://www.yiiframework.com/extension/composite-unique-key-validatable/

    Yii2 :

    // a1 needs to be unique
    ['a1', 'unique']
    // a1 needs to be unique, but column a2 will be used to check the uniqueness of the a1 value
    ['a1', 'unique', 'targetAttribute' => 'a2']
    // a1 and a2 need to be unique together, and they both will receive error message
    [['a1', 'a2'], 'unique', 'targetAttribute' => ['a1', 'a2']]
    // a1 and a2 need to be unique together, only a1 will receive error message
    ['a1', 'unique', 'targetAttribute' => ['a1', 'a2']]
    // a1 needs to be unique by checking the uniqueness of both a2 and a3 (using a1 value)
    ['a1', 'unique', 'targetAttribute' => ['a2', 'a1' => 'a3']]
    

    http://www.yiiframework.com/doc-2.0/yii-validators-uniquevalidator.html

    0 讨论(0)
  • 2020-12-29 03:14

    May be you can add this rules onto your code

    return array(
        array('name', 'unique', 'className'=>'MyModel', 'attributeName'=>'myName'),
        array('version', 'unique', 'className'=>'MyModel', 'attributeName'=>'myVersion')
    );
    
    0 讨论(0)
提交回复
热议问题