在 cakephp3 中打印 ORM 查询构建器的 SQL 查询
Print SQL query of ORM query builder in cakephp3
如何打印 ORM 查询
$query = $articles->find('all')->contain(['Comments']);
例如打印=>
SELECT * FROM comments WHERE article_id IN (comments);
使用调试函数包装您的 ORM 查询结果将显示 SQL 和绑定参数:
debug($query);
你也可以类似的用调试查看查询结果 function.See CakePHP 3: retrieving data and result sets — Debugging Queries and ResultSets
我更喜欢这个:
public function __debugInfo()
{
return [
'query' => $this->_query,
'items' => $this->toArray(),
];
}
// Print the query
debug($query->__debugInfo()['sql']);
// Prints this
SELECT * FROM comments WHERE article_id IN (comments);
那$query->sql()
呢?
$qb = $this->Person->find()->select(["id", "text" => "concat(Name,' ',Family)"])
->where(['id >' => 0])
->where($query ? ["OR" => $filters] : null)
->limit(10);
dd($qb->sql());
结果:
.../src/Controller/ClientController.php (line 86)
'SELECT Person.id AS `Person__id`, concat(Name,' ',Family) AS `text` FROM person Person WHERE (id > :c0 AND (Family like '%sam%' OR Name like '%sam%' OR Family like '%sam%' OR Name like '%sam%')) LIMIT 10'
如何打印 ORM 查询
$query = $articles->find('all')->contain(['Comments']);
例如打印=>
SELECT * FROM comments WHERE article_id IN (comments);
使用调试函数包装您的 ORM 查询结果将显示 SQL 和绑定参数:
debug($query);
你也可以类似的用调试查看查询结果 function.See CakePHP 3: retrieving data and result sets — Debugging Queries and ResultSets
我更喜欢这个:
public function __debugInfo()
{
return [
'query' => $this->_query,
'items' => $this->toArray(),
];
}
// Print the query
debug($query->__debugInfo()['sql']);
// Prints this
SELECT * FROM comments WHERE article_id IN (comments);
那$query->sql()
呢?
$qb = $this->Person->find()->select(["id", "text" => "concat(Name,' ',Family)"])
->where(['id >' => 0])
->where($query ? ["OR" => $filters] : null)
->limit(10);
dd($qb->sql());
结果:
.../src/Controller/ClientController.php (line 86)
'SELECT Person.id AS `Person__id`, concat(Name,' ',Family) AS `text` FROM person Person WHERE (id > :c0 AND (Family like '%sam%' OR Name like '%sam%' OR Family like '%sam%' OR Name like '%sam%')) LIMIT 10'