在CakePHP3中打印ORM查询构建器的SQL查询语句

7
如何打印 ORM 查询结果。
$query = $articles->find('all')->contain(['Comments']);

例如,打印 =>
SELECT * FROM comments WHERE article_id IN (comments);
3个回答

12

你知道如何只打印 SQL 部分吗? - Fury
你尝试过字符串转换吗?(string)$query - mark

11

$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'

官方的DebugKit插件拥有你所需的辅助函数 - Ilie Pandia

3

我更喜欢这个:

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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接