简体   繁体   English

如何在cakephp 3.x中执行自定义查询

[英]How to execute custom query in cakephp 3.x

Custom query execution in cakephp. 在cakephp中执行自定义查询。 I have applied below code. 我已应用以下代码。

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

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

It gives me blank array though customers table has 20 records. 虽然customers表有20条记录,但它给了我空白数组。

Please suggest me some solution. 请给我一些解决方案。

Thanks. 谢谢。

It's not recommended but somtimes there is no other way! 这不是推荐的,但有时没有别的办法! :

  1. You should mention namespace of connection manger 你应该提到连接管理器的命名空间

     use Cake\\Datasource\\ConnectionManager; 
  2. Get/initialize a connection 获取/初始化连接

     $conn = ConnectionManager::get('default'); 
  3. Execute SQL with something like this 用这样的东西执行SQL

     $stmt = $conn->execute('SELECT * FROM customers'); 
  4. Fetch the results 获取结果

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

See also 也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM