ExpressionEngine CMS
Open, Free, Amazing
Brought to you by

Thread

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

March 14, 2019 12:54pm

Subscribe [2]
  • #1 / Mar 14, 2019 12:54pm

    Richard Whitmer's avatar

    Richard Whitmer

    96 posts

    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();
  • #2 / Mar 14, 2019 2:51pm

    Kevin Cupp's avatar

    Kevin Cupp

    787 posts

    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

  • #3 / Mar 15, 2019 12:55pm

    Richard Whitmer's avatar

    Richard Whitmer

    96 posts

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

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases