From b481e8ba5cb488140ff92b7c6bc8a86dd19b6c12 Mon Sep 17 00:00:00 2001 From: Saleh Saiid Date: Tue, 24 Jun 2014 17:06:59 +0300 Subject: [PATCH 1/2] fix missing primary key --- README.md | 2 +- core/MY_Model.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 38fc601..df1c37e 100644 --- a/README.md +++ b/README.md @@ -192,7 +192,7 @@ If you'd like to customise this, you can pass through the model name as a parame class Post_model extends MY_Model { public $belongs_to = array( 'author' => array( 'model' => 'author_m' ) ); - public $has_many = array( 'comments' => array( 'model' => 'model_comments' ) ); + public $has_many = array( 'comments' => array( 'model' => 'model_comments', 'primary_key' => 'post_id' ) ); } You can then access your related data using the `with()` method: diff --git a/core/MY_Model.php b/core/MY_Model.php index 05add98..bf80377 100644 --- a/core/MY_Model.php +++ b/core/MY_Model.php @@ -482,6 +482,7 @@ public function relate($row) { $relationship = $key; $options = $value; + $options["primary_key"] = $this->primary_key; } if (in_array($relationship, $this->_with)) From 40efff0de57512a55825a53bd06764bd9ff0f120 Mon Sep 17 00:00:00 2001 From: Saleh Saiid Date: Tue, 24 Jun 2014 18:14:01 +0300 Subject: [PATCH 2/2] edit README.md for pass primary key --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index df1c37e..061737f 100644 --- a/README.md +++ b/README.md @@ -176,12 +176,21 @@ Relationships **MY\_Model** now has support for basic _belongs\_to_ and has\_many relationships. These relationships are easy to define: + class Post_model extends MY_Model { public $belongs_to = array( 'author' ); public $has_many = array( 'comments' ); } +It's better to set primary key for _belongs\_to_ and _has\_many_ : + + class Post_model extends MY_Model + { + public $belongs_to = array( 'author' => array( 'model' => 'author_m', 'primary_key' => 'author_id' ) ); + public $has_many = array( 'comments' => array( 'model' => 'model_comments', 'primary_key' => 'post_id' ) ); + } + It will assume that a MY_Model API-compatible model with the singular relationship's name has been defined. By default, this will be `relationship_model`. The above example, for instance, would require two other models: class Author_model extends MY_Model { } @@ -191,7 +200,7 @@ If you'd like to customise this, you can pass through the model name as a parame class Post_model extends MY_Model { - public $belongs_to = array( 'author' => array( 'model' => 'author_m' ) ); + public $belongs_to = array( 'author' => array( 'model' => 'author_m', 'primary_key' => 'author_id' ) ); public $has_many = array( 'comments' => array( 'model' => 'model_comments', 'primary_key' => 'post_id' ) ); }