cakephpcakephp-2.0cakephp-2.1

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


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 guess using a Model would make it a bit slower.

I found out in one forum that this can be achieved with the following code in CakePHP 1.3

$db = ConnectionManager::getInstance();
$conn = $db->getDataSource('default');
$conn->rawQuery($some_sql);

But this is not working in CakePHP 2.1.3. Any help would be appreciated. Thanks :)


Solution

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

    App::uses('ConnectionManager', 'Model');
        
    $db = ConnectionManager::getDataSource('default');
    $db->rawQuery($some_sql);