2017-04-04 36 views
1

如何使用activequery獨特的驗證在我的模型Yii2如何使用activequery獨特的驗證在我的模型

['amenityName', 'unique', 'targetClass' => Amenity::className(), 'message' => 'This amenity has already been taken.', 
       'when' => function ($model, $attribute) { 
        return $model->{$attribute} !== $model->getOldAttribute($attribute); 
       },], 
在activequery

public function active() 
{ 
     return $this->andWhere(['amenityStatus' => '1']); 
} 

    /** 
    * @inheritdoc 
    * @return Amenity[]|array 
    */ 
    public function all($db = null) 
    { 
     return parent::all($db); 
    } 

我想amenityname的獨特價值其數據處於活動狀態。現在即時通訊從所有數據是不活躍

回答

1

,你可以直接使用獨特的驗證檢查..例如,在美化型號:

public function rules() 
    { 
     return [ 
       // a1 needs to be unique in the column represented by the "a1" attribute 
       ['amenityName', 'unique'], 
       .... 

,你可以得到更多關於Infor的獨特功能驗證這裏http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html#unique
http://www.yiiframework.com/doc-2.0/yii-validators-uniquevalidator.html

+0

非常感謝你,它有很大的幫助。 –