How to execute custom query in cakephp 3.x

左心房为你撑大大i 提交于 2019-12-04 23:35:29

问题


Custom query execution in 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.

Please suggest me some solution.

Thanks.


回答1:


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

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

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

See also

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


来源:https://stackoverflow.com/questions/34553805/how-to-execute-custom-query-in-cakephp-3-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!