For example in one-to-many due to documentation (http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relational-data) you can link two models in this way (one-many = company-zone)
:
$defaultZone = new Zone;
$defaultZone->name = Zone::DEFAULT_ZONE;
$company->link('zones', $defaultZone);
But how it works for many-to-many relations when you have transit table like tbl_user_market(user_id, market_id)
?
When using a junction table for many-to-many relations, you have to
In the User model define the following relation function:
In the Market model define the following relation function:
And finally, after saving both models, link them together:
The call to
link()
will populate the junction table.Reference: http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#link()-detail