我正在尝试在 Controller (PropertyController.php)内创建一个删除函数,该函数将从数据库中删除一条记录,然后触发一个事件。
但是它抛出了这个错误:
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
堆栈跟踪:
CORE/Cake/Model/Datasource/DboSource.php line 460 → PDOStatement->execute(array)
CORE/Cake/Model/Datasource/DboSource.php line 426 → DboSource->_execute(string, array)
CORE/Cake/Model/Datasource/DboSource.php line 668 → DboSource->execute(string, array, array)
CORE/Cake/Model/Datasource/DboSource.php line 611 → DboSource->fetchAll(string, array, array)
CORE/Cake/Model/Model.php line 827 → DboSource->query(string, array, Property)
APP/Controller/PropertyController.php line 367 → Model->__call(string, array)
APP/Controller/PropertyController.php line 367 → Property->get(integer)
[internal function] → PropertyController->delete(string)
CORE/Cake/Controller/Controller.php line 490 → ReflectionMethod->invokeArgs(PropertyController, array)
CORE/Cake/Routing/Dispatcher.php line 193 → Controller->invokeAction(CakeRequest)
CORE/Cake/Routing/Dispatcher.php line 167 → Dispatcher->_invoke(PropertyController, CakeRequest)
APP/webroot/index.php line 118 → Dispatcher->dispatch(CakeRequest, CakeResponse)
我可以通过 find() 访问记录,但由于某种原因不能使用 get() 访问记录。
/属性/删除/2000
PropertyController.php 中的函数:
public function delete($propertyAgentRef = null) {
// Get property entity
$property = $this->Property->get($propertyAgentRef);
// Delete
$result = $this->Property->delete($property);
}
Property.php(模型)
class Property extends AppModel {
public $primaryKey = 'agent_ref';
public $hasMany = array(
'PropertyMediaImage' => array(
'className' => 'PropertyMediaImage',
'foreignKey' => 'agent_ref',
'dependent' => true,
'cascadeCallbacks' => true
),
'PropertyMediaFloorPlan' => array(
'className' => 'PropertyMediaFloorPlan',
'foreignKey' => 'agent_ref',
'dependent' => true,
'cascadeCallbacks' => true
),
'PropertyMediaDocument' => array(
'className' => 'PropertyMediaDocument',
'foreignKey' => 'agent_ref',
'dependent' => true,
'cascadeCallbacks' => true
),
'PropertyMediaVirtualTour' => array(
'className' => 'PropertyMediaVirtualTour',
'foreignKey' => 'agent_ref',
'dependent' => true,
'cascadeCallbacks' => true
)
);
}
我需要在模型 Controller 中添加 get 方法吗?或者这可能是我的自定义主键的问题?
最佳答案
您的代码显示您正在使用 CakePHP 2.x,但您尝试使用它的方式是 CakePHP 3。CakePHP 2.x 不具有实体对象。 Cake2 中现有的模型方法都没有作为 SQL 执行,这就是错误的原因。
仔细检查您正在阅读的文档,如果您使用 Cake2,请确保遵循 2.x 文档。
提出问题时始终说出您正在使用的确切 CakePHP 版本。
关于php - CakePHP 3.0 - $this->Model->get($id) 引发 MySQL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28586814/