Skip to content

Instantly share code, notes, and snippets.

@MichaelThessel
Created December 18, 2014 20:50
Show Gist options
  • Save MichaelThessel/f9de8f427155011cd7d9 to your computer and use it in GitHub Desktop.
Save MichaelThessel/f9de8f427155011cd7d9 to your computer and use it in GitHub Desktop.
Magento add PHP stack trace to SQL queries
diff --git a/lib/Zend/Db/Statement/Pdo.php b/lib/Zend/Db/Statement/Pdo.php
index 4530897..3561d05 100644
--- a/lib/Zend/Db/Statement/Pdo.php
+++ b/lib/Zend/Db/Statement/Pdo.php
@@ -54,6 +54,7 @@ class Zend_Db_Statement_Pdo extends Zend_Db_Statement implements IteratorAggrega
*/
protected function _prepare($sql)
{
+ $sql = $this->_trace() . $sql;
try {
$this->_stmt = $this->_adapter->getConnection()->prepare($sql);
} catch (PDOException $e) {
@@ -63,6 +64,22 @@ class Zend_Db_Statement_Pdo extends Zend_Db_Statement implements IteratorAggrega
}
/**
+ * Add a PHP stack trace comment to every SQL statement
+ *
+ * @return string Stack trace comment
+ */
+ protected function _trace() {
+ $trace = array();
+ foreach (debug_backtrace() as $call) {
+ if (isset($call['file']) && isset($call['line'])) {
+ $trace[] = $call['file'] . ' ' . $call['line'];
+ }
+ }
+
+ return "\n/*" . addslashes(implode("\n", $trace)) . "*/\n";
+ }
+
+ /**
* Bind a column of the statement result set to a PHP variable.
*
* @param string $column Name the column in the result set, either by
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment