Skip to content

Commit d06179a

Browse files
committed
Honor primary_key option in $has_many when eager loading.
Fixes #366.
1 parent d9a4130 commit d06179a

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

lib/Relationship.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ protected function set_class_name($class_name)
291291
if (!has_absolute_namespace($class_name) && isset($this->options['namespace'])) {
292292
$class_name = $this->options['namespace'].'\\'.$class_name;
293293
}
294-
294+
295295
$reflection = Reflections::instance()->add($class_name)->get($class_name);
296296

297297
if (!$reflection->isSubClassOf('ActiveRecord\\Model'))
@@ -507,7 +507,7 @@ public function load(Model $model)
507507
$fk = $this->foreign_key;
508508

509509
$this->set_keys($this->get_table()->class->getName(), true);
510-
510+
511511
$class = $this->class_name;
512512
$relation = $class::table()->get_relationship($this->through);
513513
$through_table = $relation->get_table();
@@ -604,7 +604,7 @@ public function create_association(Model $model, $attributes=array(), $guard_att
604604
public function load_eagerly($models=array(), $attributes=array(), $includes, Table $table)
605605
{
606606
$this->set_keys($table->class->name);
607-
$this->query_and_attach_related_models_eagerly($table,$models,$attributes,$includes,$this->foreign_key, $table->pk);
607+
$this->query_and_attach_related_models_eagerly($table,$models,$attributes,$includes,$this->foreign_key, $this->primary_key);
608608
}
609609
}
610610

@@ -727,4 +727,4 @@ public function load_eagerly($models=array(), $attributes, $includes, Table $tab
727727
{
728728
$this->query_and_attach_related_models_eagerly($table,$models,$attributes,$includes, $this->primary_key,$this->foreign_key);
729729
}
730-
}
730+
}

test/RelationshipTest.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function set_up($connection_name=null)
2323
Venue::$has_one = array();
2424
Employee::$has_one = array(array('position'));
2525
Host::$has_many = array(array('events', 'order' => 'id asc'));
26-
26+
2727
foreach ($this->relationship_names as $name)
2828
{
2929
if (preg_match("/$name/", $this->getName(), $match))
@@ -80,7 +80,7 @@ public function test_has_many_basic()
8080
{
8181
$this->assert_default_has_many($this->get_relationship());
8282
}
83-
83+
8484
public function test_eager_load_with_empty_nested_includes()
8585
{
8686
$conditions['include'] = array('events'=>array());
@@ -107,7 +107,7 @@ public function test_gh_256_eager_loading_three_levels_deep()
107107

108108
$this->assertEquals('Yeah Yeah Yeahs',$bill_events[0]->title);
109109
}
110-
110+
111111
/**
112112
* @expectedException ActiveRecord\RelationshipException
113113
*/
@@ -423,6 +423,23 @@ public function test_has_many_with_explicit_keys()
423423
Author::$has_many = array(array('explicit_books', 'class_name' => 'Book', 'primary_key' => 'parent_author_id', 'foreign_key' => 'secondary_author_id'));
424424
$author = Author::find(4);
425425

426+
$this->assert_equals(2, count($author->explicit_books));
427+
428+
foreach ($author->explicit_books as $book)
429+
$this->assert_equals($book->secondary_author_id, $author->parent_author_id);
430+
431+
$this->assert_true(strpos(ActiveRecord\Table::load('Book')->last_sql, "secondary_author_id") !== false);
432+
Author::$has_many = $old;
433+
}
434+
435+
public function test_has_many_with_explicit_keys_and_eager_loading()
436+
{
437+
$old = Author::$has_many;
438+
Author::$has_many = array(array('explicit_books', 'class_name' => 'Book', 'primary_key' => 'parent_author_id', 'foreign_key' => 'secondary_author_id'));
439+
$author = Author::find(4, array('include' => 'explicit_books'));
440+
441+
$this->assert_equals(2, count($author->explicit_books));
442+
426443
foreach ($author->explicit_books as $book)
427444
$this->assert_equals($book->secondary_author_id, $author->parent_author_id);
428445

0 commit comments

Comments
 (0)