在 CakePHP 3.x 中,如何计算 MySql 表中的行数?

Joe*_*e C 1 mysql cakephp-3.0

我有一个名为“devices”的 MYSQL 表。我已经成功地完成了所有的垃圾箱/蛋糕烘烤。事实上,我已经自动构建的 DevicesController.php 完全正常工作。但我不知道如何计算表中的行数。我试过了:

$conn = ConnectionManager::get('default');
$numRows = $conn->execute('select count(*) from devices');
Run Code Online (Sandbox Code Playgroud)

$this->DeviceSetups = TableRegistry::get('Devices');
$numRows = $this->Devices->query('select count(*) from devices'); // both like this
$numRows = $this->Devices->query('select count(*) from devices')->execute();  // and like this
Run Code Online (Sandbox Code Playgroud)

$this->DeviceSetups = TableRegistry::get('Devices');
$numRows = $this->Devices->find('count');
Run Code Online (Sandbox Code Playgroud)

通过 mysql_query() 并不是一个好主意,因为我已经在 app.php 中设置了所有访问信息供 CakePHP 使用。我使用 AnyModel 尝试了其他方法,但没有成功。

前 2 次尝试返回 Cake\Database\Statement\MysqlStatement,而不是表中行数的整数。我查阅了这个答案和这个答案并阅读了 CakePHP 文档。似乎没有任何东西告诉我如何对表进行计数,也没有告诉我如何执行原始 My SQL 命令字符串然后访问结果。