Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Enable to show the explanation for a query in the symfony debug bar

Add a module to run explain
Add a link to show the explain

Require the following config in databases.yml :
dev:
  propel:
    param:
      debug:
        realmemoryusage: true
        details:
          connection: { enabled: true }
  • Loading branch information...
commit 22273c9894751cd81185328463f4bdf07de98659 1 parent 8709ee5
@cedriclombardot cedriclombardot authored
View
3  config/routing.yml
@@ -0,0 +1,3 @@
+propel_debug_pannel:
+ url: /propel-debug-pannel/:connection/:base64_query
+ param: { module: sfPropelORMExplain, action: index }
View
5 config/sfPropelORMPluginConfiguration.class.php
@@ -62,6 +62,11 @@ public function initialize()
if (sfConfig::get('sf_web_debug'))
{
$this->dispatcher->connect('debug.web.load_panels', array('sfWebDebugPanelPropel', 'listenToAddPanelEvent'));
+
+ $modules = sfConfig::get('sf_enabled_modules', array());
+ $modules[] = 'sfPropelORMExplain';
+
+ sfConfig::set('sf_enabled_modules', $modules);
}
if (sfConfig::get('sf_test'))
View
58 lib/debug/sfWebDebugPanelPropel.class.php
@@ -55,6 +55,42 @@ public function getPanelContent()
<h3>Propel Version: '.Propel::VERSION.'</h3>
<ol>'.implode("\n", $this->getSqlLogs()).'</ol>
</div>
+ <script type="text/javascript">
+ function sfWebDebbugGetXMLHttpRequest() {
+ var xhr = null;
+
+ if (window.XMLHttpRequest || window.ActiveXObject) {
+ if (window.ActiveXObject) {
+ try {
+ xhr = new ActiveXObject("Msxml2.XMLHTTP");
+ } catch(e) {
+ xhr = new ActiveXObject("Microsoft.XMLHTTP");
+ }
+ } else {
+ xhr = new XMLHttpRequest();
+ }
+ } else {
+ alert("Your browser do no support XMLHTTPRequest");
+ return null;
+ }
+
+ return xhr;
+ }
+
+ function sfWebDebbugDoExplain(url, area)
+ {
+ var xhr = sfWebDebbugGetXMLHttpRequest();
+
+ xhr.onreadystatechange = function() {
+ if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
+ document.getElementById(area).innerHTML = xhr.responseText;
+ }
+ };
+
+ xhr.open("GET", url, true);
+ xhr.send(null)
+ }
+ </script>
';
}
@@ -80,7 +116,7 @@ protected function getSqlLogs()
$threshold = $config->getParameter('debugpdo.logging.details.slow.threshold', DebugPDO::DEFAULT_SLOW_THRESHOLD);
$html = array();
- foreach ($this->webDebug->getLogger()->getLogs() as $log)
+ foreach ($this->webDebug->getLogger()->getLogs() as $i => $log)
{
if ('sfPropelLogger' != $log['type'])
{
@@ -88,6 +124,7 @@ protected function getSqlLogs()
}
$details = array();
+ $explainlink = '';
$slowQuery = false;
$parts = explode($outerGlue, $log['message']);
@@ -111,10 +148,25 @@ protected function getSqlLogs()
}
}
}
+
+ // Find connection for explain
+ if ('connection' == $match[1])
+ {
+ $connection = $match[2];
+ }
}
}
// all stuff that has not been eaten by the loop should be the query string
$query = join($outerGlue, $parts);
+
+ if(isset($connection))
+ {
+ $explainlink = ', <a onclick="sfWebDebbugDoExplain(\''.sfContext::getInstance()->getRouting()->generate('propel_debug_pannel', array(
+ 'connection' => $connection,
+ 'base64_query' => base64_encode($query),
+ )).'\', \'explain_'.$i.'\')">Explain the query</a>';
+ }
+
if ($query == "SET NAMES 'utf8'")
{
// This is the initialization query that occurs on every request.
@@ -127,11 +179,13 @@ protected function getSqlLogs()
$html[] = sprintf('
<li class="%s">
<p class="sfWebDebugDatabaseQuery">%s</p>
- <div class="sfWebDebugDatabaseLogInfo">%s%s</div>
+ <div class="sfWebDebugDatabaseLogInfo">%s%s%s</div>
+ <div class="sfWebDebugDatabaseExplain" id="explain_'.$i.'"></div>
</li>',
$slowQuery ? 'sfWebDebugWarning' : '',
$query,
implode(', ', $details),
+ $explainlink,
$backtrace
);
}
View
24 modules/sfPropelORMExplain/actions/actions.class.php
@@ -0,0 +1,24 @@
+<?php
+
+class sfPropelORMExplainActions extends sfActions
+{
+ public function executeIndex(sfWebRequest $request)
+ {
+ $this->setLayout(false);
+ sfConfig::set('sf_web_debug', false);
+
+ // Open the connection
+ $con = \Propel::getConnection($request->getParameter('connection'));
+
+ // Get the adapter
+ $db = \Propel::getDB($request->getParameter('connection'));
+
+ try {
+ $this->query = base64_decode($request->getParameter('base64_query'));
+ $stmt = $db->doExplainPlan($con, $this->query);
+ $this->results = $stmt->fetchAll(\PDO::FETCH_ASSOC);
+ } catch (Exception $e) {
+ $this->getResponse()->setContent('<div class="error">This query cannot be explained.</div>');
+ }
+ }
+}
View
16 modules/sfPropelORMExplain/templates/indexSuccess.php
@@ -0,0 +1,16 @@
+<h2>Explanation</h2>
+
+<table>
+ <tr>
+ <?php foreach($results[0] as $label =>$val): ?>
+ <th><?php echo $label; ?></th>
+ <?php endforeach; ?>
+ </tr>
+ <?php foreach($results as $row): ?>
+ <tr>
+ <?php foreach($row as $item): ?>
+ <td><?php echo $item; ?></td>
+ <?php endforeach; ?>
+ </tr>
+ <?php endforeach; ?>
+</table>
Please sign in to comment.
Something went wrong with that request. Please try again.