调用未定义的方法Cake \ ORM \ Entity :: query()CakePhp

时间:2018-10-19 09:29:03

标签: php sql web cakephp orm

我正在使用cakePhp 3.x,我想调用我的控制器并进行查询,但是它引发了以下错误:“调用未定义的方法Cake \ ORM \ Entity :: query()”

我认为我做得很好,因为我已经包含了正确的库,这是我的代码:

use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
public function delete()
{
    $this->loadModel('Products');
    $conn = ConnectionManager::get('default');
    $id = !empty($_POST['id']) ? $_POST['id'] : '';
    $data = $this->request->data;
    $product = TableRegistry::get('Products')->newEntity($data);

    $query = $product->query();
    $query->delete()
    ->where(['id' => $id])
    ->execute();

    $this->autoRender = false;
}

1 个答案:

答案 0 :(得分:3)

替换此:

    $product = TableRegistry::get('Products')->newEntity($data);

与:

    $product = TableRegistry::get('Products');