Tim Tim - 2 months ago 9
PHP Question

Finder Method returning only count when calling 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.

ndm ndm
Answer

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

Comments