JOINS on activerecords

Discuss about Group-Office development here

Moderator: Developers

jamesarthur
Posts: 55
Joined: Fri Nov 08, 2013 9:15 am

JOINS on activerecords

Postby jamesarthur » Tue Nov 12, 2013 1:12 pm

Hi All

I have a question on ActiveRecords: I need to construct a query to get all comments for contacts. To do this I want to JOIN the go_model_types table to the co_comments table using model_type_id (which doesn't have a relation specified in the model). To do this I noticed that a method existed called joinModel in the FindParams query builder, which I have filled out with the correct information. My question is: how do I access those fields?

For an object attribute it would simply be

Code: Select all

$comment->id
, for example.
For an attribute from a relation join it would be

Code: Select all

$comment->category->id
, for example

PS I'm thinking of writing about my experiences of the ActiveRecord, as a sort of tutorial. Would you guys at Intermesh be interested in a copy for the wiki? :)
mschering
Site Admin
Site Admin
Posts: 8284
Joined: Tue Apr 20, 2004 1:06 pm
Location: The Netherlands - Den Bosch
Contact:

Re: JOINS on activerecords

Postby mschering » Wed Nov 13, 2013 3:28 pm

joinModel simply joins it but does not select any data. You need to add those with a separate select() call.

What you probably want is joinRelation.

Yes I'm interested in your howto!
Best regards,

Merijn Schering
Intermesh
jamesarthur
Posts: 55
Joined: Fri Nov 08, 2013 9:15 am

Re: JOINS on activerecords

Postby jamesarthur » Thu Nov 14, 2013 3:02 pm

Thanks, this gives me a hint.

The reason I cannot use joinRelation is that I want to join comments to the model_type they are comments for, and there is no relation in the comment model.

I'll post the howto on here once it's done :)

Who is online

Users browsing this forum: No registered users and 1 guest