yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.9k forks source link

PHP 7.4 ActiveRelationTrait bug. Trying to access array offset on value of type null #17914

Closed Trimod closed 4 years ago

Trimod commented 4 years ago

Hi guys!

I believe we have a bug related to PHP 7.4 upgrade. Unfortunately I was not able to reproduce it locally to get a better idea on why it happens, but I know where it happens and it's a common PHP 7.4 upgrade error, so I am posting it here in hope someone who knows related code well can clarify this and fix it.

What steps will reproduce the problem?

This is a query we have that fails from time to time (depending on DB data I guess) after we upgraded our project to PHP 7.4.2:

UserFormCapture::find()
            ->alias('ufc')
            ->joinWith([
                'userByEmail' => static function ($query) {
                    /** @var $query ActiveQuery */
                    $query->joinWith([
                        'subscription s' => static function ($query) {
                            /** @var $query ActiveQuery */
                            $query->andWhere(['s.status' => Subscription::STATUS_CANCELLED])->andWhere(new Expression('`s`.`cancelled_at` <= `ufc`.`created_at`'))->orWhere(['s.status' => null]);
                        }
                    ]);
                }
            ])
            ->where([
                'ufc.delete_at' => null,
                'ufc.place'        => UserFormCapture::getSubscriptionPlaces()
            ])
            ->andWhere(['>=', 'ufc.created_at', strtotime('-2 days')])
            ->asArray()
            ->all();

What is the expected result?

Query result returned.

What do you get instead?

yii\base\ErrorException: Trying to access array offset on value of type null in /data/www/vendor/yiisoft/yii2/db/ActiveRelationTrait.php:526

Additional info

For now we disabled eager loading for both joinWith calls, and no error cases so far. Full stack trace:

yii\\base\\ErrorException: Trying to access array offset on value of type null in \/data\/www\/vendor\/yiisoft\/yii2\/db\/ActiveRelationTrait.php:526
Stack trace:\n#0 \/data\/www\\/vendor\/yiisoft\/yii2\/db\/ActiveRelationTrait.php(526): yii\\base\\ErrorHandler->handleError()
#1 \/data\/www\/vendor\/yiisoft\/yii2\/db\/ActiveRelationTrait.php(246): yii\\db\\ActiveQuery->filterByModels()
#2 \/data\/www\/vendor\/yiisoft\/yii2\/db\/ActiveQueryTrait.php(151): yii\\db\\ActiveQuery->populateRelation()
#3 \/data\/www\/vendor\/yiisoft\/yii2\/db\/ActiveQuery.php(224): yii\\db\\ActiveQuery->findWith()
#4 \/data\/www\/vendor\/yiisoft\/yii2\/db\/Query.php(238): yii\\db\\ActiveQuery->populate()
#5 \/data\/www\/vendor\/yiisoft\/yii2\/db\/ActiveQuery.php(133): yii\\db\\Query->all()
#6 \/data\/www\/modules\/user\/commands\/tasks\/DeleteCapturesTask.php(116): yii\\db\\ActiveQuery->all()
Q A
Yii version 2.0.32
PHP version 7.4.2.
Operating system Ubuntu 18.04 LTS
yii-bot commented 4 years ago

Thanks for posting in our issue tracker. In order to properly assist you, we need additional information:

Thanks!

This is an automated comment, triggered by adding the label status:need more info.