如何在cakephp 3.x中执行自定义查询 [英] How to execute custom query in cakephp 3.x

查看:77
本文介绍了如何在cakephp 3.x中执行自定义查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cakephp中的自定义查询执行。我已应用以下代码。

Custom query execution in cakephp. I have applied below code.

$conn = ConnectionManager::get('default');

$rs = $conn->query('SELECT * FROM customers');

通过 customers 表,它为我提供了空白数组有20条记录。

It gives me blank array though customers table has 20 records.

请建议我一些解决方案。

Please suggest me some solution.

谢谢。

推荐答案

不建议这样做,但有时无法使用! :

It's not recommended but somtimes there is no other way! :


  1. 您应该提到连接管理器的命名空间

  1. You should mention namespace of connection manger

use Cake\Datasource\ConnectionManager;


  • 获取/初始化连接

  • Get/initialize a connection

    $conn = ConnectionManager::get('default');
    


  • 使用类似这样的命令执行SQL

  • Execute SQL with something like this

    $stmt = $conn->execute('SELECT * FROM customers');
    


  • 获取结果

  • Fetch the results

    $results = $stmt ->fetchAll('assoc');
    


  • 另请参见

    • Cookbook > Database Access & ORM > Database Basics > Running Select Statements
    • API > \Cake\Database\SatementInterface::fetch()
    • API > \Cake\Database\SatementInterface::fetchAll()

    这篇关于如何在cakephp 3.x中执行自定义查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆