CakePHP 3 原始 SQL 查询

2023-01-19php开发问题
2

本文介绍了CakePHP 3 原始 SQL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我使用的是 CakePHP 3,我需要对多个表运行原始 SQL 查询.在 CakePHP 2 中,这可以通过在任何模型( $this->Messages->query("select..") )上使用 query() 方法来完成.

I'm using CakePHP 3, I need to run a raw SQL query on multiple tables. In CakePHP 2, this could be done by using the query() method on any model ( $this->Messages->query("select..") ).

我需要允许我在 CakePHP 3 中运行 SQL 查询的方法.以下是我使用的代码片段:

I need the method that allows me to run a SQL query in CakePHP 3. Following is the code snippet I'm using:

$aumTable = TableRegistry::get('Messages');
$sql = "SELECT (SELECT COUNT(*) FROM `messages`) AS `Total_Count`,
        (SELECT COUNT(*) FROM `messages_output`) AS `Total_Output_Count`,
        (SELECT COUNT(*) FROM `messages_output` WHERE `is_success`=1) AS `Total_Successful_Output_Count`,
        (SELECT COUNT(*) FROM `messages_output` WHERE `is_success`=0) AS `Total_Error_Output_Count`,
        (SELECT COUNT(*) FROM `users`) AS `Total_User_Count`;";

// to run this raw SQL query what method should i use? query() doesn't work..
// $result = $aumTable->query($sql); ??
// $result = $aumTable->sql($sql); ??

如果您能提供指向 CakePHP 3 模型文档的链接,我可以在其中找到此信息,那也会很有帮助.我尝试在 google 上搜索,但只能找到与 CakePHP 2 相关的问题.

If you can provide links to CakePHP 3 model documentation where I can find this info, that would be helpful too. I tried searching on google but could only find questions related to CakePHP 2.

推荐答案

首先需要添加ConnectionManager:

First you need to add the ConnectionManager:

use CakeDatasourceConnectionManager;

然后你需要像这样获得你的连接:

Then you need to get your connection like so:

// my_connection is defined in your database config
$conn = ConnectionManager::get('my_connection');

更多信息:http://book.cakephp.org/3.0/en/orm/database-basics.html#creating-connections-at-runtime

之后,您可以像这样运行自定义查询:

After that you can run a custom query like this:

$stmt = $conn->execute('UPDATE posts SET published = ? WHERE id = ?', [1, 2]);

更多信息:http://book.cakephp.org/3.0/en/orm/database-basics.html#executing-queries

然后您就可以像这样获取行了:

And then you are ready to fetch the row(s) like this:

// Read one row.
$row = $stmt->fetch('assoc');

// Read all rows.
$rows = $stmt->fetchAll('assoc');

// Read rows through iteration.
foreach ($rows as $row) {
    // Do work
}

更多信息:http://book.cakephp.org/3.0/en/orm/database-basics.html#executing-fetching-rows

这篇关于CakePHP 3 原始 SQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17