Reputation: 51
How can i use activequery for unique validators in my model
['amenityName', 'unique', 'targetClass' => Amenity::className(), 'message' => 'This amenity has already been taken.',
'when' => function ($model, $attribute) {
return $model->{$attribute} !== $model->getOldAttribute($attribute);
},],
in activequery
public function active()
{
return $this->andWhere(['amenityStatus' => '1']);
}
/**
* @inheritdoc
* @return Amenity[]|array
*/
public function all($db = null)
{
return parent::all($db);
}
i want to get unique value of amenityname whose data is active. now im it checking from all data which is not active
Upvotes: 1
Views: 47
Reputation: 133370
you could use the unique validator directly .. eg in Amenity Model:
public function rules()
{
return [
// a1 needs to be unique in the column represented by the "a1" attribute
['amenityName', 'unique'],
....
and you can get more infor about unique validator features here http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html#unique
and http://www.yiiframework.com/doc-2.0/yii-validators-uniquevalidator.html
Upvotes: 2