2016-06-11 37 views
0

所以。我想找到這樣做的「正確」方式。我想檢索一個數據庫中所有條目的列表,以一種很好的,人類可讀的方式格式化「創建」和「修改」字段。查找方法返回時只調用全部()

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

變量$測試返回:

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

轉儲對象不一定會給出對象結構的實際表示,但可以通過the magic __debugInfo() method定義自定義的格式化調試信息。

對於結果集裝飾(這是你得到的將結果格式化時),調試信息只包含計數,即在結果集的數量,見

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

的結果集裝飾器是一個集合,並且可以迭代,就像已經使用$test一樣。 之前之間的區別呼叫all(),是前者會自動內部呼叫all(),所以最後它實際上是相同的。

您是否應該返回一個數組或結果集取決於您希望/需要您的API的行爲方式。返回一個數組將限制對結果可以做什麼,爲了應用收集方法,人們必須將數組轉換回集合,所以從性能角度來看,最好返回集合。

又見