CakePHP 2.1.x - 在AppController中运行没有任何模型的查询 [英] CakePHP 2.1.x - Run a query without any models in AppController

查看:103
本文介绍了CakePHP 2.1.x - 在AppController中运行没有任何模型的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在AppController中对没有与之相关联的模型的表上运行查询。我不想使用一个模型因为这个查询会触发每个请求,我想使用一个模型会使它有点慢。



我发现在一个论坛,这可以使用CakePHP 1.3中的以下代码实现

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

但这在CakePHP 2.1.3中不起作用。
任何帮助将不胜感激。
感谢:) $ d $ <$> 在CakePHP 2.x中是静态的,因此您应该能够使用:

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


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 :)

解决方案

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

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

这篇关于CakePHP 2.1.x - 在AppController中运行没有任何模型的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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