CakePHP 2.1.x - Run a query without any models in AppController

前端 未结 3 1331
臣服心动
臣服心动 2021-02-05 08:38

I am trying to run a query in AppController on a table that has no Model associated with it. I don\'t want to use a Model cause this query would fire on every request and I gues

相关标签:
3条回答
  • 2021-02-05 09:11

    you should run this way

        App::uses('ConnectionManager', 'Model'); 
        $db = ConnectionManager::getDataSource('default');
        if (!$db->isConnected()) {
           $this->Session->setFlash(__('Could not connect to database.'), 'default',            array('class' => 'error'));
        } else {
            $db->rawQuery($some_sql);
        }
    
    0 讨论(0)
  • 2021-02-05 09:19

    rawQuery will not return data, use $db->query instead.

    $db = ConnectionManager::getDataSource('default');
    $data = $db->query($some_sql);
    
    0 讨论(0)
  • 2021-02-05 09:31

    The getDataSource() method is static in CakePHP 2.x, so you should be able to use:

    $db = ConnectionManager::getDataSource('default');
    $db->rawQuery($some_sql);
    
    0 讨论(0)
提交回复
热议问题