Yii Framework Forum: Strange discrepancy between count($query->all()) versus $query->count() - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   Chaim L 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 12-February 14

Posted 31 December 2017 - 07:52 AM

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] =>

0

#2 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,533
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 31 December 2017 - 09:57 PM

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.
0

#3 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,500
  • Joined: 16-February 11
  • Location:Japan

Posted 02 January 2018 - 07:29 AM

Quote

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



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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users