Getting Row Counts For Related Model

Hi all I am using the listview widget and I have a data provider that looks like this

        $imageProvider = new ActiveDataProvider([

            'query' => Image::find()->where(['user_id' => $id])->with('imageComments'),

            'pagination' => [

                'pageSize' => 20,




Now I don’t actually need to return the ‘imageComments’ because I won’t be displaying them with this query. I only want to count the number of ‘imageComments’ for each Image and return that integer. How can I do this?

This is currently not possible using relations. There is a request for this on github:

However you can implement a method in your model that returns the count and use that in the gridview.

Oh I see. Implementing a method to return the count would require a another query. So I can either query the database another time to return the counts, or I could return the imageComments with the first query and use php count() to count the results (this is my original design but I realized it might not be the best solution). Which is a more efficient scenario?