{"id":1517,"date":"2022-08-30T15:17:11","date_gmt":"2022-08-30T15:17:11","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/26\/kohana-has-many-issue-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:11","modified_gmt":"2022-08-30T15:17:11","slug":"kohana-has-many-issue-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/kohana-has-many-issue-collection-of-common-programming-errors\/","title":{"rendered":"Kohana &ldquo;has many&rdquo; issue-Collection of common programming errors"},"content":{"rendered":"<p>I have a model named permission.<\/p>\n<p>Permission has many roles permission has many users permission has many denied users<\/p>\n<p>Below is from the permissions model:<\/p>\n<pre><code>protected $_has_many = array(\n    'user' =&gt; array('through' =&gt; 'user_permission'),\n    'permissiondeny' =&gt; array('model' =&gt; 'user', 'through' =&gt; 'user_permissiondeny','foreign_key' =&gt; 'permissiondeny_id'),\n    'role' =&gt; array('through' =&gt; 'role_permission'),\n);\n<\/code><\/pre>\n<p>The user and role relationships work as expected. I can select the deny permission, but when I try to add a new one I get the following php error:<\/p>\n<p>PHP Fatal error: Uncaught Database_Exception [ 1110 ]: Column &#8216;permissiondeny_id&#8217; specified twice [ INSERT INTO <code>user_permissiondeny<\/code> (<code>permissiondeny_id<\/code>, <code>permissiondeny_id<\/code>) VALUES (&#8216;1&#8217;, 1) ] ~ MODPATH\/database\/classes\/kohana\/database\/mysql.php [ 194 ]<\/p>\n<p>Any suggestions as to what I&#8217;m missing?<\/p>\n<ol>\n<li>\n<p>Since you are not following the naming conventions in ORM, you need to change your <code>has_many<\/code> declaration:<\/p>\n<pre><code>protected $_has_many = array(\n  'user' =&gt; array('through' =&gt; 'user_permission'),\n  'permissiondeny' =&gt; array('model' =&gt; 'user', 'through' =&gt; 'user_permissiondeny','foreign_key' =&gt; 'permissiondeny_id', 'far_key' =&gt; 'permission_id'),\n  'role' =&gt; array('through' =&gt; 'role_permission'),\n);\n<\/code><\/pre>\n<p>note the <code>far_key<\/code> part, you need to specify the name of the column that you&#8217;ll be looking for. may it be <code>user_id<\/code>, <code>role_id<\/code>, <code>permission_id<\/code>, etc<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-26 18:03:10. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a model named permission. Permission has many roles permission has many users permission has many denied users Below is from the permissions model: protected $_has_many = array( &#8216;user&#8217; =&gt; array(&#8216;through&#8217; =&gt; &#8216;user_permission&#8217;), &#8216;permissiondeny&#8217; =&gt; array(&#8216;model&#8217; =&gt; &#8216;user&#8217;, &#8216;through&#8217; =&gt; &#8216;user_permissiondeny&#8217;,&#8217;foreign_key&#8217; =&gt; &#8216;permissiondeny_id&#8217;), &#8216;role&#8217; =&gt; array(&#8216;through&#8217; =&gt; &#8216;role_permission&#8217;), ); The user and role relationships [&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-1517","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1517","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=1517"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1517\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}