{"id":1621,"date":"2022-08-30T15:18:03","date_gmt":"2022-08-30T15:18:03","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/kohana-2-orm-custom-primary-key-generating-an-error-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:18:03","modified_gmt":"2022-08-30T15:18:03","slug":"kohana-2-orm-custom-primary-key-generating-an-error-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/kohana-2-orm-custom-primary-key-generating-an-error-collection-of-common-programming-errors\/","title":{"rendered":"Kohana 2 ORM custom primary key generating an error-Collection of common programming errors"},"content":{"rendered":"<p>In my DB I have a <code>jobs<\/code> table, a <code>details<\/code> table, and an <code>employers<\/code> table.<\/p>\n<p><code>jobs<\/code> and <code>details<\/code> are in a one-to-one relationship, and <code>employers<\/code> and <code>details<\/code> are in a one-to-many relationship. Not all jobs have details, but all details have one employer.<\/p>\n<p>I thought it would make sense to drop the <code>id<\/code> field from <code>details<\/code>, and use the <code>job_id<\/code> field as the primary key. The DB engine is InnoDB, so primary key joins ought to be lightning fast.<\/p>\n<p>So after changing the table and adding the primary key declaration in the <code>details<\/code> model I can still access details via <code>$job-&gt;detail<\/code>. However, this line of code:<\/p>\n<pre><code>$employer = $job-&gt;detail-&gt;employer-&gt;name;\n<\/code><\/pre>\n<p>generates an error:<\/p>\n<blockquote>\n<p>Undefined index: detail_job_id<\/p>\n<\/blockquote>\n<p>I changed nothing aside from the custom primary key, so I know this is the source of the problem, but I&#8217;ve never used a custom primary key before, so I am not sure what is causing the error or what I need to fix.<\/p>\n<p>Anyone used the Kohana2 ORM with custom primary keys?<\/p>\n<ol>\n<li>\n<p>AFAIR, in v2.3.4 you can use <code>$foreign_key<\/code> property to define FK for related models. Something like <code>protected $foreign_key = array('employer' =&gt; 'detail_id');<\/code><\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 12:09:04. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>In my DB I have a jobs table, a details table, and an employers table. jobs and details are in a one-to-one relationship, and employers and details are in a one-to-many relationship. Not all jobs have details, but all details have one employer. I thought it would make sense to drop the id field from [&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-1621","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1621","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=1621"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1621\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}