无法知道 execute() 的输出 (CakePHP3.x)

It is not possible to know the output of execute() (CakePHP3.x)

我更新了数据库中的一条记录:

$res = $query->update()
        ->set(['activation' => true])
        ->where(['sha1' => $hash])
        ->execute();

请求本身被正确执行(这在您更改数据库本身时可见。)。

然后我尝试接收方法 execute():

的结果
debug($res->fetchAll('assoc'));

但是报错:数据库错误。

如果我不使用 ->fetchAll('assoc') - 一切正常,但我不知道请求是否正确完成。

我做错了什么?或者如何知道执行execute()方法的结果?

您可能想对结果调用 ->rowCount() 以查看它是否影响了一些行,或者对结果调用 ->errorInfo() 以获取任何关联的错误。由于这不是 SELECT 查询,因此 "fetch" 没有任何结果集,因此调用 ->fetchAll() 确实是错误的方法。

事实证明,为了查明是否执行了更新数据库中数据的查询,您需要:

// Create the request object
$query = $this->Users->query();

// Prepare the terms of the request
$query->update()
   ->set(['activation' => true])
   ->where(['sha1' => $hash]);

// We try to execute the query and immediately check the result
if($query->execute()->rowCount() === 1){
    // Query succeeded
} else {
    // Request failed
}