P
P
Pavel Zamoroka2015-10-21 16:59:36
Yii
Pavel Zamoroka, 2015-10-21 16:59:36

How to check for uniqueness of 2 fields in Yii2?

hello. there are 2 fields in the database: 'parent_id' and 'email'
how to check both fields?
those. there can be entries
email = [email protected] and parent_id = 1
email = [email protected] and parent_id = 2
but both values ​​cannot be repeated at the same time
If unique is specified in the model, then only email is unique, then parent_id does not play a role

public function rules()
    {
        return [
            [['parent_id'], 'integer'],
            [['email'], 'email'],
            [['email'], 'unique'],
        ];
    }

For example ['parent_id AND email'], 'unique'], but this does not work)

Decision:
[['parent_id', 'email'], 'unique', 'targetAttribute' => ['parent_id', 'email']],

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
H
holfza, 2015-10-21
@zapashok

www.yiiframework.com/doc-2.0/yii-validators-unique...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question