We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Is there a way to return a grouped result using the Model service?

Development and Programming

Richard Whitmer's avatar
Richard Whitmer
96 posts
6 years ago
Richard Whitmer's avatar Richard Whitmer

I love the new Model service, but I’m wondering how I would add a groupby statement when using it? I’m working on some custom code for an addon for a client who has channel entries that sometimes need to be grouped by the value in a custom field.

I’ve been able to do this on the front end in the templates by forking and updating the Group By extension, but I’d also like to be able to do this when fetching from the ChannelEntry Model in an addon – something like the example below, but with grouping the returned values on field_id_15

I’m not seeing any way to do that in the documentation other than using the Legacy Database Driver. Can it be done?

$entries = ee('Model')->get('ChannelEntry')
                                 ->fields('entry_id','field_id_15','entry_date')
                                 ->filter('status','open')
                                 ->filter('field_id_15','IN',array('apples','oranges','bananas','kiwi','chayote','rambutan','etc.'))
                                 ->filter('channel_id',4)
                                 ->limit(25)
                                 ->order('field_id_15','ASC')
                                 ->all();
       
Kevin Cupp's avatar
Kevin Cupp
791 posts
6 years ago
Kevin Cupp's avatar Kevin Cupp

Hi Richard. No there isn’t a way, currently, especially since GROUP BY requires aggregated columns in MySQL by default now. See here for more discussion:

https://expressionengine.com/forums/topic/248261/select-distinct-with-the-new-models

       
Richard Whitmer's avatar
Richard Whitmer
96 posts
6 years ago
Richard Whitmer's avatar Richard Whitmer

Kevin, Thanks for the reply and link to the discussion. All good to know.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.