CakePHP 3.0 - $this->Model->get($id) 抛出 MySQL 错误

CakePHP 3.0 - $this->Model->get($id) thows MySQL error

我正在尝试在控制器 (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()。

/property/delete/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
  )
);
}

我需要在模型控制器中添加 get 方法吗?还是我的自定义主键有问题?

您的代码表明您使用的是 CakePHP 2.x,但您尝试使用它的方式是 CakePHP 3。CakePHP 2.x 没有实体对象。 None Cake2 中的现有模型方法执行为 SQL,这就是错误的原因。

仔细检查您正在阅读的文档,如果您使用 Cake2,请确保遵循 2.x 文档。

问问题时总是说出您正在使用的确切 CakePHP 版本。