-
Notifications
You must be signed in to change notification settings - Fork 440
habtm via dual has_many through gives misleading exception #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I believe this should do the trick at Relationship.php line 459:
The way it is currently, it is looking for the through relationship on the related model, instead of the model where it is defined. The above code will look to the model where it is defined. |
@jmoberley I just ran into the same issue. It'd be great it you could submit a pull request for this fix. |
@bluefuton I'm working on it now but I'm new to git and github so it's a learning experience for me. |
Pull request here: |
@jmoberley Nice one. |
Closed in favor of #141 |
Model User
has_many projects, through project_users
Model ProjectUser
belongs_to project
belongs_to user
Model Project
has_many users, through project_users
Attempted code:
$user = User:find(1)
$user->projects ** Gives me the error:
Uncaught exception 'ActiveRecord\HasManyThroughAssociationException' with message 'Could not find the association project_users in model User'
So I change User model to:
Model User
has_many project_users
has_many projects, through project_users
And I get the same exception complaining about a problem in the User model...
The problem was of course:
Model Project
has_many users, through project_users
has_many project_users ** missing **
The full trace is:
Fatal error: Uncaught exception 'ActiveRecord\HasManyThroughAssociationException' with message 'Could not find the association project_users in model User' in /usr/share/php/php-activerecord/lib/Relationship.php:464
Stack trace:
#0 /usr/share/php/php-activerecord/lib/Model.php(493): ActiveRecord\HasMany->load(Object(User))
#1 /usr/share/php/php-activerecord/lib/Model.php(386): ActiveRecord\Model>read_attribute('projects')
#2 /home/mdm162/www/ntc/controllers/Timecard.controller.php(85): ActiveRecord\Model->__get('projects')
#3 /home/mdm162/www/ntc/controllers/Timecard.controller.php(60): TimecardController->printTimecard(Object(Timecard))
#4 /home/mdm162/www/ntc/controllers/Timecard.controller.php(27): TimecardController->loadTimecard()
#5 /home/mdm162/www/ntc/timecard.php(20): TimecardController->handleAction()
#6 {main} thrown in /usr/share/php/php-activerecord/lib/Relationship.php on line 464
The offending lines I believe are:
http://github.com/kla/php-activerecord/blob/master/lib/Relationship.php
Line 461
// verify through is a belongs_to or has_many for access of keys
if (!($through_relationship = $this->get_table()->get_relationship($this->through)))
throw new HasManyThroughAssociationException("Could not find the association $this->through in model " . get_class($model));
I have noticed similar behavior in exceptions when modifying has_many through with both 'class_name' and 'source'.
The modifier 'source' is not mentioned once on this documentation page:
http://www.phpactiverecord.org/projects/main/wiki/Associations
The text was updated successfully, but these errors were encountered: