php 如何在 CakePHP 中执行自定义查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22745570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute custom query in CakePHP
提问by Celebris
I'm currently trying to execute custom queries in CakePHP framework, meaning instead of using CakePHP syntax, I'd like execute normal SQL query like SELECT * FROM post ORDER BY id desc
.
我目前正在尝试在 CakePHP 框架中执行自定义查询,这意味着我不想使用 CakePHP 语法,而是执行像SELECT * FROM post ORDER BY id desc
.
I cannot figure out how to do it. I read several answers to the similar questions, but it still doesn't work.
我不知道该怎么做。我阅读了类似问题的几个答案,但它仍然不起作用。
As far as I understand I should put function like:
据我了解,我应该把功能如下:
public function testx()
{
$sql = "SELECT * FROM posts WORDER by id desc";
return $this->query($sql);
}
to file Post
in directory Model
and then put this code:
到目录Post
中的文件Model
,然后把这个代码:
$result = $this->Post->testx();
to index
function in PostsController
in Controller
directory.
要index
在功能上PostsController
的Controller
目录。
I still can't figure out how to print out the data in View/Posts/index.ctp
.
我仍然无法弄清楚如何打印出View/Posts/index.ctp
.
Thanks in advance for any answer.
提前感谢您的任何回答。
回答by K.Raj.
public function index(){
$this->loadModel('Post'); //or you can load it in beforeFilter()
$data=$this->Post->query('SELECT * FROM posts ORDER by id desc');
$this->set('data',$data);
}
In your view file index.ctp. Write Below Code
在您的视图文件index.ctp 中。写在下面的代码
<?php
if($data) {
echo "<pre>";
print_r($data);
} else {
echo 'no data found';
}
?>
回答by Gurpreet Singh
If you want to execute a delete (or any other) query without a model then you should try
如果您想在没有模型的情况下执行删除(或任何其他)查询,那么您应该尝试
$db = ConnectionManager::getDataSource('default');
$db->rawQuery("DELETE FROM table WHERE id=5");
回答by Gurpreet Singh
App::import("Model", "ModelName");
$model = new ModelName();
$query = "Select * from tablename";
$data = $model->query($query);
回答by Mayank
You can use connection manager to run custom SQL queries. Follow this article from cake php documentation to use connection manager: https://book.cakephp.org/3.0/en/orm/database-basics.html
您可以使用连接管理器来运行自定义 SQL 查询。按照 cake php 文档中的这篇文章使用连接管理器:https: //book.cakephp.org/3.0/en/orm/database-basics.html
use Cake\Datasource\ConnectionManager;
$connection = ConnectionManager::get('default');
$results = $connection->execute('SELECT * FROM articles')->fetchAll('assoc');
After this to view results in your view (index.ctp) you have to set the result variable
在此之后要在您的视图 (index.ctp) 中查看结果,您必须设置结果变量
$this->set('result',$result);
Now you can view the data from your database in index.ctp using the result variable.
现在,您可以使用 result 变量在 index.ctp 中查看数据库中的数据。
回答by Sonia Bhatia
$this->Post->query("SELECT * FROM posts ORDER by id desc");
回答by PHP Team
No need to make such extra function testx()in this case. You can also use the custom query directly in your index function like,
在这种情况下不需要制作这样的额外函数 testx()。您还可以直接在索引函数中使用自定义查询,例如,
public function index(){
$data=$this->Post->query('SELECT * FROM posts ORDER by id desc');
$this->set('data',$data);
}
and in your index.ctp filejust print the data,
并在您的index.ctp 文件中打印数据,
<?php print_r($data); ?>
Hope this will help you.
希望这会帮助你。
回答by cornelb
Controller
控制器
$result = $this->Post->testx();
$this->set('posts', $result);
View
看法
debug($posts);
foreach ($posts as $post) {
// ...
}