Strange discrepancy between count($query->all()) versus $query->count()

Strange problem here. Wondering if anybody else encountered anything similar.

I have an ActiveDataProvider with an ActiveQuery. The generated SQL seems to be accurate, and when I use $query->count() to count the records, I get 272. However, if I perform $query->all() and then count the records, I get a different number:




echo $dataProvider->query->count(); // 272

echo count($dataProvider->query->all()); // 256 (!!)



What can be going on here??

This is the ActiveDataProvider:




yii\data\ActiveDataProvider Object

(

    [query] => app\models\ReservationItemQuery Object

        (

            [resolution] => 

            [sql] => 

            [on] => 

            [joinWith] => 

            [select] => 

            [selectOption] => 

            [distinct] => 

            [from] => 

            [groupBy] => 

            [join] => Array

                (

                    [0] => Array

                        (

                            [0] => INNER JOIN

                            [1] => reservation

                            [2] => reservation.id = reservation_item.reservation_id

                        )


                    [1] => Array

                        (

                            [0] => INNER JOIN

                            [1] => user

                            [2] => user.id = reservation.user_id

                        )


                    [2] => Array

                        (

                            [0] => LEFT JOIN

                            [1] => reservation_team

                            [2] => reservation_team.reservation_item_id = reservation_item.id

                        )


                )


            [having] => 

            [union] => 

            [params] => Array

                (

                )


            [_events:yii\base\Component:private] => Array

                (

                )


            [_behaviors:yii\base\Component:private] => Array

                (

                )


            [where] => Array

                (

                    [0] => and

                    [1] => Array

                        (

                            [{{%reservation_item}}.event_id] => 37

                            [{{%reservation_item}}.status] => 1

                        )


                    [2] => Array

                        (

                            [0] => =

                            [1] => {{%reservation_item}}.type

                            [2] => 4

                        )


                )


            [limit] => -1

            [offset] => 

            [orderBy] => 

            [indexBy] => 

            [emulateExecution] => 

            [modelClass] => app\models\events\EventOptionOrder

            [with] => 

            [asArray] => 

            [multiple] => 

            [primaryModel] => 

            [link] => 

            [via] => 

            [inverseOf] => 

        )


    [key] => 

    [db] => 

    [id] => 

    [_sort:yii\data\BaseDataProvider:private] => yii\data\Sort Object

        (

            ...

        )


    [_pagination:yii\data\BaseDataProvider:private] =>



it could be your joins that are messing up your count, try running the query without joins and compare to see if you get the same count, then put them back you have mix of inner joins and outer join order could potentially fix this.

I guess that "query->count()" counts the count of rows in the result set of the query which may have duplicated entries of the main model. And "count(query->all())" counts the number of Active Record model instances that are populated with the result set but without the duplicated entries.

In other words, “count(‘id’)” instead of “count()” (that is equivalent to “count(’*’)”) may give you the right result of 256, I guess.

Anyway, I’d like to see how do you construct the query, and the raw SQL performed to get the result.