php 如何在 CakePHP 中创建自定义 MySQL 查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15170852/
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 create custom MySQL queries in CakePHP?
提问by Johnathan Au
I am trying to create my own MySQL queries in Cakephp.
我正在尝试在 Cakephp 中创建我自己的 MySQL 查询。
This is my LocationsController.php:
这是我的LocationsController.php:
<?php
App::uses('Location', 'Model');
class LocationsController extends AppController
{
public $helpers = array('Html', 'Form');
function index()
{
$this->loadModel("Location");
$this->Location->get();
}
}
This is my LocationModel.php:
这是我的LocationModel.php:
<?php
App::uses('AppModel', 'Model');
class LocationModel extends Model {
public $name = 'Location';
public function get()
{
$this->Location->query("SELECT * FROM locations;");
}
}
As you can see, I am just trying to perform a simple query but it doesn't work. I get this error:
如您所见,我只是尝试执行一个简单的查询,但它不起作用。我收到此错误:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error
in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'get' at line 1
When I use one of the magic methods like find("all") instead, it works...
当我使用 find("all") 等魔术方法之一时,它会起作用......
Can you see what the problem is? I really can't and I'm only trying to do a simple task!
你能看出问题是什么吗?我真的不能,我只是想做一个简单的任务!
回答by thaJeztah
The class name of your Location model should be Location, not LocationModel.
您的 Location 模型的类名应该是Location,而不是LocationModel。
Because of this, CakePHP will generate a 'generic' model for the Locations database table and use that model instead of your own model. Because this generic model does nothave a get()method, it will execute getas a SQL statement, causing the error
因此,CakePHP 将为 Locations 数据库表生成一个“通用”模型,并使用该模型而不是您自己的模型。因为通用模型并不能有一个get()方法,它会执行get一个SQL语句,导致错误
Also, inside the Model, you should not use $this->Location->query();, but simply $this->query();
此外,在模型内部,您不应该使用$this->Location->query();, 而只是$this->query();
回答by Karma
Location Controller should be:
位置控制器应该是:
<?php
App::uses('Location', 'Model'); // Y do u need this?
class LocationsController extends AppController
{
public $helpers = array('Html', 'Form');
function index()
{
$this->loadModel("Location");
$this->LocationModel->getLocations(); // I will strongly discourage using get()
}
}
Location Model should be:
位置模型应该是:
<?php
App::uses('AppModel', 'Model');
class LocationModel extends Model {
public $name = 'Location';
public function getLocations() // Try to avoid naming a function as get()
{
/** Choose either of 2 lines below **/
return $this->query("SELECT * FROM locations;"); // if table name is `locations`
return $this->query("SELECT * FROM Location;"); // if table name is `Location` since your public name is `Location`
}
}

