Skip to content

Commit aa7f78a

Browse files
committed
Honor primary_key option in $belongs_to.
Fixes #364.
1 parent d06179a commit aa7f78a

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/Relationship.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,9 @@ public function load(Model $model)
685685
*/
686686
class BelongsTo extends AbstractRelationship
687687
{
688+
689+
static protected $valid_association_options = array('primary_key');
690+
688691
public function __construct($options=array())
689692
{
690693
parent::__construct($options);
@@ -695,6 +698,9 @@ public function __construct($options=array())
695698
//infer from class_name
696699
if (!$this->foreign_key)
697700
$this->foreign_key = array(Inflector::instance()->keyify($this->class_name));
701+
702+
if (!isset($this->primary_key) && isset($this->options['primary_key']))
703+
$this->primary_key = is_array($this->options['primary_key']) ? $this->options['primary_key'] : array($this->options['primary_key']);
698704
}
699705

700706
public function __get($name)

test/RelationshipTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ public function test_belongs_to_with_explicit_foreign_key()
165165
Book::$belongs_to = $old;
166166
}
167167

168+
public function test_belongs_to_with_explicit_primary_key()
169+
{
170+
$old = Book::$belongs_to;
171+
Book::$belongs_to = array(array('explicit_author', 'class_name' => 'Author', 'primary_key' => 'parent_author_id'));
172+
173+
$book = Book::find(1);
174+
$this->assert_equals(1, $book->author_id);
175+
$this->assert_equals(1, $book->explicit_author->parent_author_id);
176+
$this->assert_equals(3, $book->explicit_author->author_id);
177+
178+
Book::$belongs_to = $old;
179+
}
180+
168181
public function test_belongs_to_with_select()
169182
{
170183
Event::$belongs_to[0]['select'] = 'id, city';

0 commit comments

Comments
 (0)