Skip to content
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

habtm via dual has_many through gives misleading exception #68

Closed
mdm162 opened this issue Jul 19, 2010 · 6 comments
Closed

habtm via dual has_many through gives misleading exception #68

mdm162 opened this issue Jul 19, 2010 · 6 comments

Comments

@mdm162
Copy link

mdm162 commented Jul 19, 2010

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

@jmoberley
Copy link

I believe this should do the trick at Relationship.php line 459:


if (!($through_relationship = $model->table()->get_relationship($this->through)))

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.

@bluefuton
Copy link

@jmoberley I just ran into the same issue. It'd be great it you could submit a pull request for this fix.

@jmoberley
Copy link

@bluefuton I'm working on it now but I'm new to git and github so it's a learning experience for me.

@jmoberley
Copy link

Pull request here:
#141

@bluefuton
Copy link

@jmoberley Nice one.

Fuitad added a commit to Fuitad/php-activerecord that referenced this issue Apr 22, 2011
jmoberley pushed a commit to jmoberley/php-activerecord that referenced this issue Jul 27, 2011
@al-the-x
Copy link
Collaborator

Closed in favor of #141

koenpunt added a commit that referenced this issue Jul 19, 2014
koenpunt added a commit that referenced this issue Jul 19, 2014
koenpunt added a commit that referenced this issue Jul 21, 2014
koenpunt added a commit that referenced this issue Nov 17, 2014
koenpunt added a commit that referenced this issue Dec 6, 2014
koenpunt added a commit to koenpunt/php-activerecord that referenced this issue Dec 6, 2014
koenpunt added a commit that referenced this issue Dec 7, 2014
koenpunt added a commit that referenced this issue Jan 18, 2015
koenpunt added a commit that referenced this issue Jun 12, 2017
koenpunt added a commit that referenced this issue Jun 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants