Skip to content

Instantly share code, notes, and snippets.

@siran
Last active May 8, 2017 15:27
Show Gist options
  • Save siran/afeb1ca069500fd5566c4bb02dbc57e9 to your computer and use it in GitHub Desktop.
Save siran/afeb1ca069500fd5566c4bb02dbc57e9 to your computer and use it in GitHub Desktop.
How to get complete sql query with values replaced in the placeholders (CakePHP 3.4)
<?php
use Cake\Database\Log\QueryLogger;
use Cake\ORM\Query;
class myLog extends QueryLogger
{
public function toStatement(Query $query)
{
$sql = $query->sql();
$bindings = $query->valueBinder()->bindings();
foreach ($bindings as $k => $value) {
$params[$value['placeholder']] = $value['value'];
}
$query->params = $params;
$query->query = $sql;
if (!empty($query->params)) {
$query->query = $this->_interpolate($query);
}
return $query->query;
}
}
//...
$datos = $MyModel->find()
->select($myFields)
->where($conditions);
$ql = new myLog();
debug($ql->toStatement($datos));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment