G
G
gerrper2014-09-07 16:59:42
Yii
gerrper, 2014-09-07 16:59:42

Yii2 many-to-many with three tables. How to organize?

There are tables:
- Author
- Document
it is clear that there is a Many-to-Many relationship.
You need to create a third table, Author-Document-relation , which will store author_id:doc_id, i.e. relations.
The question is how to organize a selection in the third table using the ActiveQuery::hasMany() method?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2014-09-07
@gerrper

$this->hasMany(
    Document::className(),
    ['id' => 'document_id']
)->viaTable(
    'author_documents',
    ['author_id' => 'id']
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question