Skip to content

Commit 05b3277

Browse files
authored
Merge pull request #436 from jpfuentes2/jmoberley-issue_68
relationship references wrong table
2 parents 39343c1 + 9c43bdb commit 05b3277

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

lib/Relationship.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ public function load(Model $model)
496496
if ($this->through)
497497
{
498498
// verify through is a belongs_to or has_many for access of keys
499-
if (!($through_relationship = $this->get_table()->get_relationship($this->through)))
499+
if (!($through_relationship = $model->table()->get_relationship($this->through)))
500500
throw new HasManyThroughAssociationException("Could not find the association $this->through in model " . get_class($model));
501501

502502
if (!($through_relationship instanceof HasMany) && !($through_relationship instanceof BelongsTo))
@@ -508,8 +508,7 @@ public function load(Model $model)
508508

509509
$this->set_keys($this->get_table()->class->getName(), true);
510510

511-
$class = $this->class_name;
512-
$relation = $class::table()->get_relationship($this->through);
511+
$relation = $model::table()->get_relationship($this->through);
513512
$through_table = $relation->get_table();
514513
$this->options['joins'] = $this->construct_inner_join_sql($through_table, true);
515514

test/HasManyThroughTest.php

+37
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,41 @@ public function test_gh107_has_many_though_include_eager_with_namespace()
5555
$this->assert_equals(1, $user->id);
5656
$this->assert_equals(1, $user->newsletters[0]->id);
5757
}
58+
59+
public function test_gh68_has_many_through_with_missing_associations()
60+
{
61+
Property::$has_many = array(
62+
array('amenities', 'through' => 'property_amenities')
63+
);
64+
Amenity::$has_many = array(
65+
array('properties', 'through' => 'property_amenities')
66+
);
67+
68+
$property = Property::find('first');
69+
try{
70+
$property->amenities;
71+
$this->fail('Should trigger exception');
72+
}catch(ActiveRecord\HasManyThroughAssociationException $e){
73+
$this->assertEquals('Could not find the association property_amenities in model Property', $e->getMessage());
74+
}
75+
}
76+
77+
public function test_gh68_has_many_through_with_missing_association()
78+
{
79+
Property::$has_many = array(
80+
'property_amenities',
81+
array('amenities', 'through' => 'property_amenities')
82+
);
83+
Amenity::$has_many = array(
84+
array('properties', 'through' => 'property_amenities')
85+
);
86+
87+
$amenity = Amenity::find('first');
88+
try{
89+
$amenity->properties;
90+
$this->fail('Should trigger exception');
91+
}catch(ActiveRecord\HasManyThroughAssociationException $e){
92+
$this->assertEquals('Could not find the association property_amenities in model Amenity', $e->getMessage());
93+
}
94+
}
5895
}

test/RelationshipTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,14 @@ public function test_has_many_through()
341341

342342
public function test_gh27_has_many_through_with_explicit_keys()
343343
{
344+
Property::$has_many = array(
345+
'property_amenities',
346+
array('amenities', 'through' => 'property_amenities')
347+
);
348+
Amenity::$has_many = array(
349+
'property_amenities'
350+
);
351+
344352
$property = Property::first();
345353

346354
$this->assert_equals(1, $property->amenities[0]->amenity_id);

0 commit comments

Comments
 (0)