cakephp3 自己的查询生成器

cakephp3 own query builder

我是 cakephp 的新手,我正在关注教程,我来自其他语言,我不常阅读这样的查询:

   public function findTagged(Query $query, array $options)
    {
        $columns = [
            'Articles.id', 'Articles.user_id', 'Articles.title',
            'Articles.body', 'Articles.published', 'Articles.created',
            'Articles.slug',
        ];

        $query = $query
            ->select($columns)
            ->distinct($columns);

        if (empty($options['tags'])) {
            // If there are no tags provided, find articles that have no tags.
            $query->leftJoinWith('Tags')
                ->where(['Tags.title IS' => null]);
        } else {
            // Find articles that have one or more of the provided tags.
            $query->innerJoinWith('Tags')
                ->where(['Tags.title IN' => $options['tags']]);
        }

        return $query->group(['Articles.id']);
    }

这是一个简单的查询,很容易理解,但是如果我有一个更复杂的查询,有很多连接等,是否有可能用 sql sintax 编写你自己的查询,你可以吗帮我把这段代码翻译成用 sql?

编写的查询

谢谢

您可以使用 $connection->execute() (https://book.cakephp.org/3.0/en/orm/database-basics.html#running-select-statements) 直接执行 SQL 查询,但我建议坚持使用 cakephp 的 ORM。

如果您想知道上面发布的查询如何转换为 SQL,我建议您使用 DebugKit。如果您的应用程序配置中有 debug = true,当您在浏览器中打开您的应用程序时,您将在右下角看到这个红色矩形。单击它并单击 "Sql queries":您将在其中某处找到上面查询生成的 SQL。或者,您可以使用查询日志记录(请参阅此处:https://book.cakephp.org/3.0/en/orm/database-basics.html#database-query-logging