diff --git a/lib/Relationship.php b/lib/Relationship.php index 702d6b80c..cd7a7a820 100644 --- a/lib/Relationship.php +++ b/lib/Relationship.php @@ -510,8 +510,7 @@ public function load(Model $model) { $this->set_keys($this->get_table()->class->getName(), true); - $class = $this->class_name; - $relation = $class::table()->get_relationship($this->through); + $relation = $model::table()->get_relationship($this->through); $through_table = $relation->get_table(); $this->options['joins'] = $this->construct_inner_join_sql($through_table, true); diff --git a/test/RelationshipTest.php b/test/RelationshipTest.php index 467726e29..f3f8622b2 100644 --- a/test/RelationshipTest.php +++ b/test/RelationshipTest.php @@ -297,6 +297,15 @@ public function test_has_many_through() $this->assert_equals(3,$hosts[1]->id); } + public function test_gh68_has_many_through_without_inverse() + { + $property = Property::first(); + $amenities = $property->amenities_2->getArrayCopy(); + + $this->assert_equals(1, $amenities[0]->amenity_id); + $this->assert_equals(2, $amenities[1]->amenity_id); + } + public function test_gh27_has_many_through_with_explicit_keys() { $property = Property::first(); diff --git a/test/models/Property.php b/test/models/Property.php index e0f913cc9..7a6959ce6 100644 --- a/test/models/Property.php +++ b/test/models/Property.php @@ -6,7 +6,9 @@ class Property extends ActiveRecord\Model static $has_many = array( 'property_amenities', - array('amenities', 'through' => 'property_amenities') + array('amenities', 'through' => 'property_amenities'), + array('property_amenities_2', 'class_name' => 'PropertyAmenity'), + array('amenities_2', 'class_name' => 'Amenity', 'through' => 'property_amenities_2'), ); }; ?>