Yii2 : how to check value is unique with below scenario

141 views Asked by At

my table and its data

id   |type | email
1    |1    | [email protected]
2    |1    | [email protected]
3    |2    | [email protected]
4    |2    | [email protected]
5    |2    | [email protected]

Their is lots of type 1,2,3,4,5,..

All TYPE has dublicate emails except type 2

but,I want to make column email unique only for type 2 using yii2 model validation

1

There are 1 answers

0
Insane Skull On BEST ANSWER

You can use filter and try something like this, not tested though

['email', 'unique', 'targetClass' => Model::className(), 'filter' => function ($query) {
    return $query->andWhere(['type' => 2]);
}],