MySQL and the "HAVING" expression, how do you do it in CakePHP? PDF Print E-mail
User Rating: / 0
PoorBest 
CakePHP - Basics
Written by Stuart Duncan   
Saturday, 30 January 2010 18:46

Lately I've been needing to create queries using the "HAVING" expression so that I could find pictures that have received 5 or more votes towards their rating... there are various ways to do that but I really needed to have it use the "HAVING" expression, so I hit the information super highway in search of CakePHP articles and not surprisingly, there were not very many off ramps.

I have a working solution now but it's sort of a hack/fix since CakePHP apparently feels that it's not an expression worth including into their vast number of array options. Here's how I did it: The "HAVING" expression is unique in that it needs to be placed directly after the GROUP BY expression, rather than mixed in with the WHERE stuff.... so, the logical solution is simply this:

$this->Model->find('all', array(
'group' => 'Photo.id HAVING count(Photo.rating) > 5'
));

As I said, it's a bit of a hack/fix in that you're tagging it in with another expression but you know what? It works.

Hopefully CakePHP will adress this in the future but until then, feel free to use this.

 

Add comment


Security code
Refresh

Login to TyCamTech.com

Donate

Must have Pepsi, feed my addiction here:


Recommended Books

How To Flip Websites Home Study Course.
Complete Video Instruction On How To Quickly Set And And Flip (i.e. Sell) Websites For Profit. 6 X Video Lessons Plus Bonuses -- Great Value For Buyer And Maximum 75% Payout For Affiliate!

Introduction to Programming
A Quick And Easy Ebook That Introduces Readers To Computer Programming In A Jargon Free, Easy To Understand Way.

Setting up a Web Server
Teaches All The Steps To Hosting A Website From Home.

Follow us on



Follow tycamtech on Twitter