2016-06-11 5 views
0

So. Ich möchte den "richtigen" Weg finden, dies zu tun. Ich möchte eine Liste aller Einträge in einer Datenbank abrufen, formatiere die Felder "erstellt" und "geändert" in einer netten, menschenlesbaren Weise.Finder Methode, die nur beim Aufruf aller zurückgibt()

In CakePHP2.x hätte ich die AfterFind-Methode verwendet. Da es in CakePHP3 nichts davon gibt, habe ich mich an blog post gewandt und festgestellt, dass ich die formatResults-Funktion verwenden musste. So natürlich habe ich versucht, diese (und viele andere Iterationen der gleichen Sache):

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

Die Variable $ Test liefert:

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' 

} 

} 

die Variable $ r Rückgabe:

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' 

} 

Nach The Query builder part of the book sollte all() die Ergebnismenge zurückgeben. Wenn jedoch $test->all() heißt ich diese Überraschung:

object(Cake\Datasource\ResultSetDecorator) { 

'count' => (int) 2 

} 

Könnte jemand bitte mich in die richtige Richtung? Ich bin wirklich verwirrt, und ich kann nur die toArray-Methode verwenden, aber ich möchte immer noch wissen, warum das nicht funktioniert, da ich immer noch das neue ORM-System lerne.

Antwort

1

Das Dumping von Objekten gibt nicht unbedingt eine tatsächliche Darstellung der Objektstruktur, sondern benutzerdefinierte formatierte Debuginformationen, die über the magic __debugInfo() method definiert sind.

Für Ergebnismenge Dekorateure (das ist, was Sie bekommen, wenn Ergebnis Formatter Anwendung), die Debug-Informationen enthält nur die Zählung, dh die Anzahl der Ergebnisse in der Gruppe finden

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

Die result set decorator ist eine Sammlung und kann genau wie bei $test iteriert werden. Der Unterschied zwischen es vor und nach Aufruf all(), ist, dass der ehemalige intern all() automatisch aufrufen wird, so dass es im Endeffekt effektiv das gleiche ist.

Ob Sie ein Array oder eine Ergebnismenge zurückgeben sollten, hängt davon ab, wie Ihre API sich verhalten soll. Die Rückgabe eines Arrays begrenzt die Möglichkeiten, die mit den Ergebnissen erzielt werden können. Um die Sammlungsmethoden anzuwenden, muss das Array wieder in eine Sammlung konvertiert werden. Daher wäre es zum Beispiel aus Sicht der Leistung besser, die Sammlung zurückzugeben.

Siehe auch

Verwandte Themen