Home  >  Article  >  Backend Development  >  Common methods for Yii debugging SQL, yii debugging sql_PHP tutorial

Common methods for Yii debugging SQL, yii debugging sql_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:23:18782browse

Common methods for Yii debugging SQL, yii debugging sql

Yii debugging SQL mainly has the following methods:

1. The system comes with debugging:

First index.php turns on debugging mode:

// remove the following lines when in production mode 
defined('YII_DEBUG') or define('YII_DEBUG',true); 
// specify how many levels of call stack should be shown in each log message 
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3); 
//app use time 
//defined('YII_BEGIN_TIME') or define('YII_BEGIN_TIME',microtime(true));

main.php page:

'errorHandler'=>array( 
  // use 'site/error' action to display errors 
  'errorAction'=>'site/error', 
), 
'log'=>array( 
  'class'=>'CLogRouter', 
  'routes'=>array( 
    array( 
      'class'=>'CFileLogRoute', 
      'levels'=>'error, warning', 
    ), 
    // 下面显示页面日志 
    array( 
      'class'=>'CWebLogRoute', 
      'levels'=>'trace',   //级别为trace 
      'categories'=>'system.db.*' //只显示关于数据库信息,包括数据库连接,数据库执行语句 
    ),    
  ), 
),

The larger the number of YII_TRACE_LEVEL, the clearer the information

2. Use debugging tools to debug:

Unzip the yii-debug-toolbar package and put it in extensions, then add

at the end of the configuration file main.php
'log'=>array( 
   'class'=>'CLogRouter', 
   'routes'=>array( 
     array( 
       'class'=>'ext.yii-debug-toolbar.YiiDebugToolbarRoute', 
       'ipFilters'=>array('127.0.0.1','192.168.1.215'), 
     ), 
   ), 
 ),

If it does not appear, add two attributes in the db under 'components',

'enableProfiling'=>true, 
'enableParamLogging'=>true,

If there are plug-ins for other debugging tools, conflicts may occur and the sql statement cannot be output. Just comment out that code.

(Urgent) Convert the result array of yii SQL query into AR mode

In addition to cactivedataprovider, you can also use CArrayDataProvider
I will give you an example:
In controller:
$rawdata = Yii::app()->db->createCommand('select * from {{post}} where id52e58ba9390274e4429e04ac2ee1be63queryAll();

$data = new CArrayDataProvider($rawdata, array(
'id' => 'user',
'sort' => array(
'attributes' => array(
'id', 'title', 'content'
),
),
' pagination' => array(
'pageSize' => 10,
),
));
$this->render('gridview_array', array('dataProvider' => ; $data));

Then in the view:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'somegrid' ,
'dataProvider'=>$dataProvider,
'columns'=>array(
'id',
'title',
'content',
),
));
That’s ok~

How to debug SQL functions

Just experiment one by one and see which result returns what you want. This self-explanatory language has no so-called debugging methods. It’s done with just one command. How do you think it’s debugging?

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/840746.htmlTechArticleCommon methods for Yii debugging SQL, yii debugging sql Yii debugging SQL mainly has the following methods: 1. The system comes with debugging : First index.php turns on debugging mode: // remove the following lines when...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn