Finder方法在调用all()时仅返回计数



所以。我想找到做这件事的"正确"方法。我想检索数据库中所有条目的列表,以一种良好的、可读的方式格式化"创建"one_answers"修改"字段。

在Cakephp2.x中,我会使用afterFind方法。由于Cakephp3中没有这些,我转向这篇博客文章,发现我必须使用formatResults函数。所以很自然,我尝试了这个(以及同一事物的许多其他迭代):

public function findAllForView(Query $query, array $options)
{
$test = $query->formatResults(function ($results) {
$r = $results->map(function ($row) {
$row['created'] = new Time($row['created']);
$row['created'] = $row['created']->nice();
$row['modified'] = new Time($row['modified']);
$row['modified'] = $row['modified']->nice();
return $row;
});
return $r;
});
debug($test);
foreach ($test as $t) {
debug($t);
}
debug($test->all());
return $test;
}

变量$test返回:

object(CakeORMQuery) {
'(help)' => 'This is a Query object, to get the results execute or iterate it.',
'sql' => 'SELECT Casinos.id AS `Casinos__id`, Casinos.name AS `Casinos__name`, Casinos.address AS `Casinos__address`, Casinos.address2 AS `Casinos__address2`, Casinos.city AS `Casinos__city`, Casinos.province AS `Casinos__province`, Casinos.country AS `Casinos__country`, Casinos.latitude AS `Casinos__latitude`, Casinos.longitude AS `Casinos__longitude`, Casinos.created AS `Casinos__created`, Casinos.modified AS `Casinos__modified` FROM casinos Casinos',
'params' => [],
'defaultTypes' => [
'Casinos__id' => 'integer',
'Casinos.id' => 'integer',
'id' => 'integer',
'Casinos__name' => 'string',
'Casinos.name' => 'string',
'name' => 'string',
'Casinos__address' => 'string',
'Casinos.address' => 'string',
'address' => 'string',
'Casinos__address2' => 'string',
'Casinos.address2' => 'string',
'address2' => 'string',
'Casinos__city' => 'string',
'Casinos.city' => 'string',
'city' => 'string',
'Casinos__province' => 'string',
'Casinos.province' => 'string',
'province' => 'string',
'Casinos__country' => 'string',
'Casinos.country' => 'string',
'country' => 'string',
'Casinos__latitude' => 'float',
'Casinos.latitude' => 'float',
'latitude' => 'float',
'Casinos__longitude' => 'float',
'Casinos.longitude' => 'float',
'longitude' => 'float',
'Casinos__created' => 'datetime',
'Casinos.created' => 'datetime',
'created' => 'datetime',
'Casinos__modified' => 'datetime',
'Casinos.modified' => 'datetime',
'modified' => 'datetime'
],
'decorators' => (int) 0,
'executed' => false,
'hydrate' => true,
'buffered' => true,
'formatters' => (int) 1,
'mapReducers' => (int) 0,
'contain' => [],
'matching' => [],
'extraOptions' => [],
'repository' => object(AppModelTableCasinosTable) {
'registryAlias' => 'Casinos',
'table' => 'casinos',
'alias' => 'Casinos',
'entityClass' => 'AppModelEntityCasino',
'associations' => [
(int) 0 => 'users'
],
'behaviors' => [
(int) 0 => 'Timestamp'
],
'defaultConnection' => 'default',
'connectionName' => 'default'
}
}

变量$r返回:

object(AppModelEntityCasino) {
'id' => (int) 1,
'name' => 'Test Casino',
'address' => 'Somewhere avenue',
'address2' => null,
'city' => 'Somewhere',
'province' => 'Province',
'country' => 'Alwaysland',
'latitude' => (float) 51.1644,
'longitude' => (float) -114.093,
'created' => 'Jun 8, 2016, 10:04 PM',
'modified' => 'Jun 8, 2016, 10:04 PM',
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'created' => true,
'modified' => true
],
'[original]' => [
'created' => object(CakeI18nFrozenTime) {
'time' => '2016-06-08T22:04:53+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'modified' => object(CakeI18nFrozenTime) {
'time' => '2016-06-08T22:04:55+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
}
],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Casinos'
}

根据本书的查询生成器部分,all()应该返回结果集。然而,当$test->all()被调用时,我得到了一个惊喜:

object(CakeDatasourceResultSetDecorator) {
'count' => (int) 2
}

有人能告诉我正确的方向吗?我真的很困惑,我可能只是使用toArray方法,但我仍然想知道为什么这不起作用,因为我仍在学习新的ORM系统。

转储对象不一定能为您提供对象结构的实际表示,而是通过神奇的__debugInfo()方法定义的自定义格式调试信息。

对于结果集装饰器(这是应用结果格式化程序时得到的),调试信息只包含计数,即集合中的结果数,请参阅

https://github.com/cakephp/cakephp/blob/3.2.10/src/Collection/Collection.php#L95-L100

结果集装饰器是一个集合,可以像使用$test一样进行迭代。在之前调用all()之后执行此操作的区别在于,前者将在内部自动调用all(),因此最终实际上是相同的。

您应该返回数组还是结果集,取决于您希望/需要API的行为方式。返回数组将限制对结果的处理,为了应用集合方法,必须将数组转换回集合,因此从性能的角度来看,最好返回集合。

另请参见

  • Cookbook>数据库访问&ORM>查询生成器>从表中选择行
  • 食谱>数据库访问&ORM>查询生成器>查询如何懒散地评估

最新更新