Part of the EllisLab Network
   
 
Active Record usage for advanced query
Posted: 01 June 2009 03:34 PM   [ Ignore ]  
Summer Student
Total Posts:  5
Joined  08-08-2008

Hi all,

I love the idea of Active Records and would like to keep this in my application, however, I cannot figure out how to construct the following query:

SELECT *
FROM (

SELECT DISTINCT clients.first_nameclients.last_nameSUMuser.off_court_rate * ( (
timesheet.offcourt_end_time timesheet.offcourt_start_time
) /60 /60 ) ) AS offcourt_totalSUMuser.on_court_rate * ( (
timesheet.oncourt_end_time timesheet.oncourt_start_time
) /60 /60 ) ) AS oncourt_total
FROM 
`clients`
LEFT JOIN timesheet ON timesheet.client_id clients.id
LEFT JOIN user ON user
.id timesheet.user_id
GROUP BY clients
.id
)total
WHERE total
.oncourt_total IS NOT NULL
OR total.offcourt_total IS NOT NULL 

Now, I can create the sub query using this:

$this->db->select("SUM(user.off_court_rate*((timesheet.offcourt_end_time - timesheet.offcourt_start_time)/60/60)) as offcourt_total, SUM(user.on_court_rate*((timesheet.oncourt_end_time - timesheet.oncourt_start_time)/60/60)) as oncourt_total");
                
$this->db->from('timesheet');
                
$this->db->join('user','user.id=timesheet.user_id','left'); 

But how do I wrap THAT around another select query? My only idea is to just use a manual query for this but I’d rather not. I don’t even think my usage of Active Records is good for the sub query i wrote.

Anybody have any ideas on what I should change/do?

thanks!

-Prateek

Profile
 
 
Posted: 01 June 2009 05:19 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  155
Joined  06-06-2007

Yeah, I have similar issues.

I recommend separating into custom query models so all your non AR stuff is in one place if you need to switch DB providers.

 Signature 

Codeigniter - Saving Kittens from Drupal

Web Construction Engineer - NPC Digital - My Site

Profile
 
 
Posted: 01 June 2009 05:48 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  5
Joined  08-08-2008

Figured as much. I just did a custom query to move along in the project, hopefully someone knows of a way!

Profile
 
 
Posted: 01 June 2009 06:06 PM   [ Ignore ]   [ # 3 ]  
Lab Assistant
RankRank
Total Posts:  211
Joined  10-22-2003

Maybe you can create a view in your database to simplify the Active Record code?  I’m going through the same thing and trying to decide if I want to use the AR pattern for everything except complex queries or to drop it altogether.

 Signature 

Template Driven PHP Shopping Cart Software

Profile
 
 
Posted: 01 June 2009 06:08 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  5
Joined  08-08-2008

My only problem with that is the subquery will be variable. It’ll have custom date ranges set by the user (which i don’t think i included in the query above) so a view wouldn’t work. Otherwise it’d be the PERFECT solution :D

Thanks!

-Prateek

Profile
 
 
Posted: 01 June 2009 06:21 PM   [ Ignore ]   [ # 5 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3175
Joined  06-11-2007

Sub-queries are not currently supported with ActiveRecord syntax. ActiveRecord exists purely to speed up simple queries. For more advanced queries you either need to use pure SQL or take a look at a ORM library like DataMapper.

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

Profile
 
 
Posted: 01 June 2009 09:00 PM   [ Ignore ]   [ # 6 ]  
Lab Assistant
RankRank
Total Posts:  211
Joined  10-22-2003

Someone wrote an ORM for CI and it is in the wiki.  You might want to check it out.

 Signature 

Template Driven PHP Shopping Cart Software

Profile