Open Mikk36 opened 1 year ago
$testQuery = new \yii\db\Expression("select entity.id from entity where entity.parent_id is null and entity.status = 10"); $test = Entity::find()->andWhere("id in ({$testQuery})")->count();
// or $testQuery = Entity::findBySql("select entity.id from entity where entity.parent_id is null and entity.status = 10"); $expression = new \yii\db\Expression($testQuery->sql); $test = Entity::find()->andWhere("id in ({$expression})")->count();
// or $testQuery = Entity::findBySql("select entity.id from entity where entity.parent_id is null and entity.status = 10"); $test = Entity::find()->andWhere(["in" , "id", $testQuery->all()])->count();
// or $testQuery = Entity::findBySql("select entity.id from entity where entity.parent_id is null and entity.status = 10"); $expression = new \yii\db\Expression($testQuery->sql); $test = Entity::find()->andWhere("id in ({$expression})")->count();
This is ok as long as You know that that specific query is crafted from raw SQL, however is very annoying to keep track of when this sort of query is an ActiveRecord class method for finding some other related models.
What steps will reproduce the problem?
What is the expected result?
What do you get instead?
This seems to come from https://github.com/yiisoft/yii2/blob/f388ca71b08b89e940f1ffbe4afa19ae9d5e115f/framework/db/QueryBuilder.php#L227 not taking into account the possibility of Query already having sql in place. It should do a similar check to this: https://github.com/yiisoft/yii2/blob/f388ca71b08b89e940f1ffbe4afa19ae9d5e115f/framework/db/ActiveQuery.php#L351
Additional info