Clark Stuth Thu Nov 10 11:30:46 -0500 2011

Subject: having issues with $has_one_through

Hey All,

I'm trying to get started with PHP ActiveRecord, and really like it so far. I've read all of the documentation on the site, and a good majority of the forum posts, but am unable to figure out my problem.

I am trying to set up a $has_one_through relationship in my database. Here is the pertinent database information (I didn't make these tables, but am trying to make ActiveRecord work now that I am re-factoring the code):

support tickets:
support_ticket_id

ticket_user_assigned:
ticket_user_assigned_id
user_id
position

users:
user_id

In the ticket_user_assigned table, the last record inserted has a "last" in the position column, to make querying easier.

Here are my class definitions, and what I am trying to do: http://pastie.org/2842477

When I try loading up the assigned_user reference, I get the following error:

Fatal error: Uncaught exception 'ActiveRecord\HasManyThroughAssociationException' with message 'Could not find the association last_assigned_user in model SupportTickets' in /var/www/vendor/ActiveRecord/lib/Relationship.php:491
Stack trace:
#0 /var/www/vendor/ActiveRecord/lib/Model.php(485): ActiveRecord\HasMany->load(Object(SupportTickets))
#1 /var/www/vendor/ActiveRecord/lib/Model.php(333): ActiveRecord\Model->read_attribute('assigned_user')
#2 /home/clark/Test_Projects/PHP_ActiveRecord/test.php(8): ActiveRecord\Model->__get('assigned_user')
#3 {main}
thrown in /var/www/vendor/ActiveRecord/lib/Relationship.php on line 491

Any idea what I am doing wrong? All of my other associations have worked, except for when I try using the "through" option.

Thanks in advance.


Yoan B Fri Nov 11 03:30:50 -0500 2011

try removing "last_assigned_user" for a try.

Clark Stuth Fri Nov 11 10:23:49 -0500 2011

Yoan,

Thanks for the suggestion. I've tried removing the last_user_assigned association, but am still having issues. Here is what my models look like now: http://pastie.org/2847704

I am then trying the following:

$ticket = SupportTickets::first();
print_r($ticket->assigned_users);

But I am still getting the same error:

Fatal error: Uncaught exception 'ActiveRecord\HasManyThroughAssociationException' with message 'Could not find the association ticket_user_assigned in model SupportTickets' in /var/www/vendor/ActiveRecord/lib/Relationship.php:491
Stack trace:
#0 /var/www/vendor/ActiveRecord/lib/Model.php(485): ActiveRecord\HasMany->load(Object(SupportTickets))
#1 /var/www/vendor/ActiveRecord/lib/Model.php(333): ActiveRecord\Model->read_attribute('assigned_users')
#2 /home/clark/Test_Projects/PHP_ActiveRecord/test.php(8): ActiveRecord\Model->__get('assigned_users')
#3 {main}
thrown in /var/www/vendor/ActiveRecord/lib/Relationship.php on line 491

Clark Stuth Fri Nov 11 10:30:15 -0500 2011

I have also tested each individual relationship, without using the through statement. Given the above models, the following does work:

$ticket = SupportTickets::first(); print_r($ticket->ticket_user_assigned[0]->user);
Clark Stuth Fri Nov 11 10:48:06 -0500 2011

So, I was able to get this working, but not in the way I expected. I needed to define ticket_user_assigned as a $has_many association, then the assigned_user association as $has_one with the through, as well as the join condition.

Here are the models now: http://pastie.org/2847820

So running the following then works:

$ticket = SupportTickets::first();
print_r($ticket->assigned_user);

(1-4/4)