php - Finder Method returning only count when calling all() -
so. want find "correct" way of doing this. retrieve a list of entries in database, format "created" , "modified" fields in nice, human readable way.
in cakephp2.x, have used afterfind method. seeing there none of in cakephp3, turned blog post , found had use formatresults function. naturally, tried (and many other iterations of 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 $t) { debug($t); } debug($test->all()); return $test; }
the variable $test returns:
object(cake\orm\query) { '(help)' => 'this query object, results execute or iterate it.', 'sql' => 'select casinos.id `casinos__id`, casinos.name `casinos__name`, casinos.address `casinos__address`, casinos.address2 `casinos__address2`, casinos.city `casinos__city`, casinos.province `casinos__province`, casinos.country `casinos__country`, casinos.latitude `casinos__latitude`, casinos.longitude `casinos__longitude`, casinos.created `casinos__created`, casinos.modified `casinos__modified` 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 the query builder part of book , all()
should return result set. however, when $test->all()
called surprise:
object(cake\datasource\resultsetdecorator) { 'count' => (int) 2 }
could please point me in right direction? i'm confused, , may use toarray method, i'd still know why isn't working, still learning new orm system.
dumping objects doesn't give actual representation of objects structure, custom formatted debug information, defined via the magic __debuginfo()
method.
for result set decorators (which when applying result formatters), debug info contains count, ie number of results in set, see
https://github.com/cakephp/cakephp/blob/3.2.10/src/collection/collection.php#l95-l100
the result set decorator collection, , can iterated you're doing $test
already. difference between doing before , after calling all()
, former internally call all()
automatically, in end it's same.
whether should return array or result set, depends on how want/need api behave. returning array limit can done results, in order apply collection methods 1 have convert array collection, example performance point of view better return collection.
see also
Comments
Post a Comment