php - CakePHP 3.0 - $this->Model->get($id) 引发 MySQL 错误

标签 php mysql cakephp

我正在尝试在 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/

相关文章:

php - 如何才能让每个电影标题只显示一次?

php - 我应该如何开始使用基于 PHP zend 框架 OOP 的网站

php - 将 Microsoft Access 数据库与 Silverstripe 网站数据库同步

php - 将左连接表行作为嵌套数组的 Cakephp 自定义查询

php - 当用户从 CakePHP 的下拉列表中选择时,在表单中填充 <div>

php - 自动检测类的自动加载器存在问题

php - 安全 header 无效 - Paypal 错误 - WHMCS

php - 如何引用刚刚通过自动增量插入的外键到两个插入查询

mysql - 使用 SQL 从 MySQL 数据库结果集中选择最近的周围行(上方/下方)

database - CakePHP - 如何将翻译行为应用于现有数据库?