dongshuo8756 2016-06-11 03:10
浏览 26
已采纳

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

So. I want to find the "correct" way of doing this. I would like to retrieve a a list of all the entries in a database, format the "created" and "modified" fields in a nice, human readable way.

In Cakephp2.x, I would have used the afterFind method. Seeing as there is none of that in Cakephp3, I turned to this blog post and found that I had to use the formatResults function. So naturally, I tried this (And many other iterations of the same thing):

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;
}

The variable $test returns:

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'

}

}

The variable $r returns:

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'

}

According to The Query builder part of the book , all() should return the result set. However, When $test->all() is called I get this surprise:

object(Cake\Datasource\ResultSetDecorator) {

'count' => (int) 2

}

Could someone please point me in the right direction? I'm really confused, and I may just use the toArray method, but I'd still like to know why this isn't working, as I am still learning the new ORM system.

  • 写回答

1条回答 默认 最新

  • dongmiyu8979 2016-06-11 15:13
    关注

    Dumping objects doesn't necessarily give you an actual representation of the objects structure, but custom formatted debug information, defined via the magic __debugInfo() method.

    For result set decorators (which is what you get when applying result formatters), the debug info only contains the count, ie the number of results in the set, see

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

    The result set decorator is a collection, and can be iterated just like you're doing it with $test already. The difference between doing it before and after calling all(), is that the former will internally call all() automatically, so in the end it's effectively the same.

    Whether you should return an array or a result set, depends on how you want/need your API to behave. Returning an array will limit what can be done with the results, in order to apply collection methods one would have to convert the array back into a collection, so from for example a performance point of view it would be better to return the collection.

    See also

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题