{"id":8004,"date":"2015-11-19T00:48:58","date_gmt":"2015-11-19T00:48:58","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/19\/how-do-i-work-with-many-to-many-relations-in-yii2-open-source-projects-yiisoft-yii2\/"},"modified":"2022-08-30T15:03:04","modified_gmt":"2022-08-30T15:03:04","slug":"how-do-i-work-with-many-to-many-relations-in-yii2-open-source-projects-yiisoft-yii2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/19\/how-do-i-work-with-many-to-many-relations-in-yii2-open-source-projects-yiisoft-yii2\/","title":{"rendered":"How do I work with many-to-many relations in Yii2-open source projects yiisoft\/yii2"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/680124f6821dca1b603cf5e513b2088a?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>vim<\/strong><\/p>\n<p>When using a junction table for many-to-many relations, you have to<\/p>\n<ol>\n<li>Define the relations<\/li>\n<li>Link the two models together<\/li>\n<\/ol>\n<p>In the User model define the following relation function:<\/p>\n<pre><code>public function getMarkets() {\n    return $this-&gt;hasMany(Market::className(), ['id' =&gt; 'market_id'])\n      -&gt;viaTable('tbl_user_market', ['user_id' =&gt; 'id']);\n}\n<\/code><\/pre>\n<p>In the Market model define the following relation function:<\/p>\n<pre><code>public function getUsers() {\n    return $this-&gt;hasMany(User::className(), ['id' =&gt; 'user_id'])\n      -&gt;viaTable('tbl_user_market', ['market_id' =&gt; 'id']);\n}\n<\/code><\/pre>\n<p>And finally, after saving both models, link them together:<\/p>\n<pre><code>$user = new User;\n$user-&gt;name = 'Foo';\n$user-&gt;save();\n\n$market = new Market;\n$market-&gt;name = 'Bar';\n$market-&gt;save();\n\n$user-&gt;link('markets', $market);\n<\/code><\/pre>\n<p>The call to <code>link()<\/code> will populate the junction table.<\/p>\n<p>Reference: http:\/\/www.yiiframework.com\/doc-2.0\/yii-db-baseactiverecord.html#link()-detail<\/p>\n","protected":false},"excerpt":{"rendered":"<p>vim When using a junction table for many-to-many relations, you have to Define the relations Link the two models together In the User model define the following relation function: public function getMarkets() { return $this-&gt;hasMany(Market::className(), [&#8216;id&#8217; =&gt; &#8216;market_id&#8217;]) -&gt;viaTable(&#8216;tbl_user_market&#8217;, [&#8216;user_id&#8217; =&gt; &#8216;id&#8217;]); } In the Market model define the following relation function: public function getUsers() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8004","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8004"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8004\/revisions"}],"predecessor-version":[{"id":8706,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8004\/revisions\/8706"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}