New way to GROUP BY in CakePHP

Just recently ( end of May, 2008 ) there was an addition to CakePHP’s find method to easily build a GROUP BY in your SQL.

If you’ve been using something like $this->Product->find(‘all’… etc., etc. Now you can add a new key ‘group’ to your find method, like so:

$this->Product->find(‘all’,array(‘fields’=>array(‘Product.type’,’MIN(Product.price) as price’), ‘group’=> ‘Product.type’));

Of course you will have to upgrade to a nightly SVN core for this to work.

About these ads

4 Responses to New way to GROUP BY in CakePHP

  1. ragrawal says:

    hi,

    Thanks for the tip. I have been using CakePHP for almost more than a year but never found a clean solution to “group by” problem, until today.

  2. teknoid says:

    @ragrawal

    Good to hear. You’re welcome.

  3. Olamma says:

    Hi i tried the new group by as you have it above but for some reason it doesnt seem to work for me.

  4. teknoid says:

    @Olamma

    It’s possible that you are using an older version of CakePHP, where this option is not supported.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 25 other followers

%d bloggers like this: