Print out a SQL single query (Yii 1.x)

半城伤御伤魂 提交于 2020-01-14 12:36:33

问题


I have a massive query that is generated using CDbCriteria as shown below:-

$schema = Yii::app()->db->schema;
$builder = $schema->commandBuilder;

// how to echo out this query?
$command = $builder->createFindCommand($schema->getTable('myuser'), $criteria);
$results = $command->queryAll();

I know I can use the 'logging' feature of Yii to view the query, is it possible to just echo out this single query (as opposed to having Yii show me tons of other queries that are being run on the page).


回答1:


you can print query built by query builder by using $command->text. In your example code will be:

    $schema = Yii::app()->db->schema;
    $builder = $schema->commandBuilder;
    $criteria = new CDbCriteria();
    $command = $builder->createFindCommand($schema->getTable('name_of_table'), $criteria);
    $results = $command->text;
    echo $results;

$command->text will return your complete query text




回答2:


Add this in your config file. You can see the query and other details at the bottom of the page.

'db'=>array(
        'enableProfiling'=>true,
        'enableParamLogging' => true,
),
'log'=>array(
    'class'=>'CLogRouter',
    'routes'=>array(
        …
        array(
            'class'=>'CProfileLogRoute',
            'levels'=>'profile',
            'enabled'=>true,
        ),
    ),
),


来源:https://stackoverflow.com/questions/33344202/print-out-a-sql-single-query-yii-1-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!