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.