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

Closures in Querys no longer possible since 2.0.51-dev #20194

Closed SusanneWaletzko closed 5 months ago

SusanneWaletzko commented 5 months ago

What steps will reproduce the problem?

I have some search models in legacy code that are using closures. Worked fine until update on 2.0.51-dev

What is the expected result?

Search model contains something like

$query = User::find(); 
$dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
// confederationId
 if ($this->confederationId) {
         $query->joinWith(['userProfile' => function ($q) {
                $q->andFilterWhere(['user_profiles.confed_id' => $this->confederationId]);
         }]);
 }

expected search query to be extended and search by e.g. "confederationId"

What do you get instead?

Exception
Serialization of 'Closure' is not allowed
1. in ..../releases/893/vendor/yiisoft/yii2/db/Query.php at line 1399
139013911392139313941395139613971398139914001401        ]);
    }

    /**
     * Returns the SQL representation of Query
     * @return string
     */
    public function __toString()
    {
        return serialize($this);
    }
}

Additional info

In 2.0.50

seems not to be used?

Q A
Yii version 2.0.51-dev
PHP version 8.1.28
Operating system
lav45 commented 5 months ago

It seems that method \yii\db\Query::__toString() has never been used.