简体   繁体   English

Yii 1,获取由CDbCriteria生成的SQL

[英]Yii 1, get SQL generated by CDbCriteria

got a problem, I need to get all ids of models (not only visible but all models) in CGridView. 有一个问题,我需要在CGridView中获取所有模型ID(不仅可见,而且所有模型)。 For that I use its DataProvider - get a criteria from it and pass it to command builder 为此,我使用其DataProvider-从中获取条件并将其传递给命令生成器

$criteria = $dataProvider->getCriteria();
$criteria->select = 'id';
$command = Y::db()->commandBuilder->createFindCommand('tableName', $criteria);
$ids = $command->queryColumn();

It is working fine until we got filtering via related tables. 在我们通过相关表进行过滤之前,它一直工作良好。 For example user adds a number to a filter of the grid - "House Number" = 24. When it happens related table - "address" adds to $criteria->with and "address.home_number = 24" adds to $criteria->condition . 例如,用户在网格过滤器中添加了一个数字-“房屋编号” =24。当发生相关表时,“地址”添加到$criteria->with而“ address.home_number = 24”添加到$criteria->condition

ActiveRecord automatically parses the "with" property of criteria and apply joins, so our condition would be fine with it, but CommandBuilder - not. ActiveRecord会自动解析条件的“具有”属性并应用联接,因此我们的条件可以满足要求,但CommandBuilder-可以。 I can't use AR for it, since it is deadly slow. 我无法使用AR,因为它的速度非常慢。 I must use Builder, but I can't make joins, there could be more than 20 filters applied. 我必须使用Builder,但无法进行联接,可能会应用20多个过滤器。

If I could get SQL generated by AR and then pass it to Builder - it would be great. 如果我能得到AR生成的SQL,然后将其传递给Builder,那就太好了。

After some research I've created custom class ActiveFinder - copy of CActiveFinder class of Yii framework. 经过研究后,我创建了自定义类ActiveFinder-Yii框架的CActiveFinder类的副本。 So you can just pass model you are gonna search and the criteria in new static method: 因此,您只需传递要搜索的模型和新静态方法中的条件即可:

$model = new User();
$criteria = new CDbCriteria();
$criteria->select = 't.id';
$command = ActiveFinder::getCommand($model, $criteria);
// $command variable have the SQL text
$sql = $command->text;

here you can grab the class - https://github.com/LinGG/ActiveFinder 在这里您可以上课-https://github.com/LinGG/ActiveFinder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM