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

时间:2016-06-11 03:10:07

标签: php cakephp orm query-builder cakephp-3.x

因此。我想找到这样做的“正确”方法。我想检索数据库中所有条目的列表,以一种漂亮的,可读的方式格式化“已创建”和“已修改”字段。

在Cakephp2.x中,我会使用afterFind方法。看到Cakephp3中没有,我转向这个blog post,发现我必须使用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(Cake\ORM\Query) {

'(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(App\Model\Table\CasinosTable) {

    'registryAlias' => 'Casinos',
    'table' => 'casinos',
    'alias' => 'Casinos',
    'entityClass' => 'App\Model\Entity\Casino',
    'associations' => [
        (int) 0 => 'users'
    ],
    'behaviors' => [
        (int) 0 => 'Timestamp'
    ],
    'defaultConnection' => 'default',
    'connectionName' => 'default'

}

}

变量$ r返回:

object(App\Model\Entity\Casino) {

'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(Cake\I18n\FrozenTime) {

        'time' => '2016-06-08T22:04:53+00:00',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    'modified' => object(Cake\I18n\FrozenTime) {

        'time' => '2016-06-08T22:04:55+00:00',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    }
],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Casinos'

}

根据The Query builder part of the bookall()应返回结果集。但是,当调用$test->all()时,我会感到惊讶:

object(Cake\Datasource\ResultSetDecorator) {

'count' => (int) 2

}

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

1 个答案:

答案 0 :(得分:1)

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

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

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

结果集装饰器是一个集合,可以像你使用$test一样进行迭代。 之前 之后 {<1>}之间的区别在于前者会在内部自动调用all(),所以最后它实际上是相同。

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

另见