You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExplainAction.php 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\debug\actions\db;
  8. use yii\base\Action;
  9. use yii\debug\panels\DbPanel;
  10. use yii\web\HttpException;
  11. /**
  12. * ExplainAction provides EXPLAIN information for SQL queries
  13. */
  14. class ExplainAction extends Action
  15. {
  16. /**
  17. * @var DbPanel
  18. */
  19. public $panel;
  20. public function run($seq, $tag)
  21. {
  22. $this->controller->loadData($tag);
  23. $timings = $this->panel->calculateTimings();
  24. if (!isset($timings[$seq])) {
  25. throw new HttpException(404, 'Log message not found.');
  26. }
  27. $query = $timings[$seq]['info'];
  28. $results = $this->panel->getDb()->createCommand('EXPLAIN ' . $query)->queryAll();
  29. $output[] = '<table class="table"><thead><tr>' . implode(array_map(function($key) {
  30. return '<th>' . $key . '</th>';
  31. }, array_keys($results[0]))) . '</tr></thead><tbody>';
  32. foreach ($results as $result) {
  33. $output[] = '<tr>' . implode(array_map(function($value) {
  34. return '<td>' . (empty($value) ? 'NULL' : htmlspecialchars($value)) . '</td>';
  35. }, $result)) . '</tr>';
  36. }
  37. $output[] = '</tbody></table>';
  38. return implode($output);
  39. }
  40. }