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

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测