Qiang Xue
11 years ago
6 changed files with 219 additions and 54 deletions
@ -0,0 +1,75 @@
|
||||
<?php |
||||
|
||||
namespace yii\debug\models\search; |
||||
|
||||
use yii\data\ArrayDataProvider; |
||||
use yii\debug\components\search\Filter; |
||||
|
||||
/** |
||||
* Db represents the model behind the search form about current request database queries. |
||||
*/ |
||||
class Db extends Base |
||||
{ |
||||
|
||||
/** |
||||
* @var string type attribute input search value |
||||
*/ |
||||
public $type; |
||||
|
||||
/** |
||||
* @var integer query attribute input search value |
||||
*/ |
||||
public $query; |
||||
|
||||
public function rules() |
||||
{ |
||||
return [ |
||||
[['type', 'query'], 'safe'], |
||||
]; |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function attributeLabels() |
||||
{ |
||||
return [ |
||||
'type' => 'Type', |
||||
'query' => 'Query', |
||||
]; |
||||
} |
||||
|
||||
/** |
||||
* Returns data provider with filled models. Filter applied if needed. |
||||
* @param array $params |
||||
* @param array $models |
||||
* @return \yii\data\ArrayDataProvider |
||||
*/ |
||||
public function search($params, $models) |
||||
{ |
||||
$dataProvider = new ArrayDataProvider([ |
||||
'allModels' => $models, |
||||
'pagination' => [ |
||||
'pageSize' => 10, |
||||
], |
||||
'sort' => [ |
||||
'attributes' => ['duration','type','query'], |
||||
'defaultOrder' => [ |
||||
'duration' => SORT_DESC, |
||||
], |
||||
], |
||||
]); |
||||
|
||||
if (!($this->load($params) && $this->validate())) { |
||||
return $dataProvider; |
||||
} |
||||
|
||||
$filter = new Filter(); |
||||
$this->addCondition($filter, 'type', true); |
||||
$this->addCondition($filter, 'query', true); |
||||
$dataProvider->allModels = $filter->filter($models); |
||||
|
||||
return $dataProvider; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,53 @@
|
||||
<?php |
||||
use yii\helpers\Html; |
||||
use yii\grid\GridView; |
||||
?> |
||||
<h1>Database Queries</h1> |
||||
|
||||
<?php |
||||
|
||||
echo GridView::widget([ |
||||
'dataProvider' => $dataProvider, |
||||
'id' => 'db-panel-detailed-grid', |
||||
'filterModel' => $searchModel, |
||||
'filterUrl' => $panel->getUrl(), |
||||
'columns' => [ |
||||
['class' => 'yii\grid\SerialColumn'], |
||||
[ |
||||
'attribute' => 'duration', |
||||
'value' => function ($data) |
||||
{ |
||||
return sprintf('%.1f ms',$data['duration']); |
||||
}, |
||||
], |
||||
[ |
||||
'attribute' => 'type', |
||||
'value' => function ($data) |
||||
{ |
||||
return Html::encode(mb_strtoupper($data['type'],'utf8')); |
||||
}, |
||||
], |
||||
[ |
||||
'attribute' => 'query', |
||||
'value' => function ($data) |
||||
{ |
||||
$query = Html::encode($data['query']); |
||||
|
||||
if (!empty($data['trace'])) { |
||||
$query .= Html::ul($data['trace'], [ |
||||
'class' => 'trace', |
||||
'item' => function ($trace) { |
||||
return "<li>{$trace['file']}({$trace['line']})</li>"; |
||||
}, |
||||
]); |
||||
} |
||||
return $query; |
||||
}, |
||||
'format' => 'html', |
||||
'options' => [ |
||||
'width' => '70%', |
||||
], |
||||
] |
||||
], |
||||
]); |
||||
?> |
@ -0,0 +1,7 @@
|
||||
<?php if ($queryCount): ?> |
||||
<div class="yii-debug-toolbar-block"> |
||||
<a href="$url" title="Executed <?php echo $queryCount; ?> database queries which took <?php echo $queryTime; ?>.">
|
||||
DB <span class="label"><?php echo $queryCount; ?></span> <span class="label"><?php echo $queryTime; ?></span>
|
||||
</a> |
||||
</div> |
||||
<?php endif; ?> |
Loading…
Reference in new issue