How to access to an object data php cakephp 3.1 -
this cakephp 3.1 query result, whant access directly $data->items error saying key item not found , don't whant use funtcion toarray() because if return 'items' => ... , lost 'query' => ... informations. can me right syntaxe $data->items or $data->{'items'} ... .
$data = object(cake\orm\resultset) { 'query' => object(cake\orm\query) { '(help)' => 'this query object, results execute or iterate it.', 'sql' => 'select posts_types.id `posts_types__id`, posts_types.name `posts_types__name` posts_types posts_types order id asc', 'params' => [], 'defaulttypes' => [ 'posts_types.id' => 'integer', 'id' => 'integer', 'posts_types.name' => 'string', 'name' => 'string' ], 'decorators' => (int) 0, 'executed' => true, 'hydrate' => true, 'buffered' => true, 'formatters' => (int) 0, 'mapreducers' => (int) 0, 'contain' => [], 'matching' => [], 'extraoptions' => [], 'repository' => object(cake\orm\table) { 'registryalias' => 'posts_types', 'table' => 'posts_types', 'alias' => 'posts_types', 'entityclass' => '\cake\orm\entity', 'associations' => [], 'behaviors' => [], 'defaultconnection' => 'default', 'connectionname' => 'default' } }, 'items' => [ (int) 0 => object(cake\orm\entity) { 'id' => (int) 1, 'name' => 'descriptif de la formation', '[new]' => false, '[accessible]' => [ '*' => true ], '[dirty]' => [], '[original]' => [], '[virtual]' => [], '[errors]' => [], '[repository]' => 'posts_types' }, (int) 1 => object(cake\orm\entity) { 'id' => (int) 2, 'name' => 'cours', '[new]' => false, '[accessible]' => [ '*' => true ], '[dirty]' => [], '[original]' => [], '[virtual]' => [], '[errors]' => [], '[repository]' => 'posts_types' }, (int) 2 => object(cake\orm\entity) { 'id' => (int) 3, 'name' => 'evénements', '[new]' => false, '[accessible]' => [ '*' => true ], '[dirty]' => [], '[original]' => [], '[virtual]' => [], '[errors]' => [], '[repository]' => 'posts_types' } ] }
there no items property
the items key in question result of calling toarray
, is direct answer you've asked.
as may obvious looking @ public methods the result set class, , the collection trait uses, none of properties of result set intended directly accessed.
here's simple example demonstrating how use result sets:
use cake\orm\tableregistry; $resultset = tableregistry::get('posts'); // iterate foreach($resultset $postobject) { echo $postobject->title; } // obtain data array $arrayofpostobjects = $resultset->toarray(); echo get_class($resultset); // still result set
note calling methods on resultset not change - still result set, there no loss of information. there's misunderstanding in question because it's not clear you're doing it's not clear misunderstanding is.
what getting query?
in comments you've asked:
if want $data->query should do?
this application should never do, developer you'd want part of debugging. classes implement __debuginfo - 2 keys "query" , "items" come when debug/var_dumping result set object:
/** * returns array can used describe internal state of * object. * * @return array */ public function __debuginfo() { return [ 'query' => $this->_query, 'items' => $this->toarray(), ]; }
so query, thing need debug/var_dump object. mentioned shows "items" result of calling toarray
.
note log queries configure query logging, developers should using debug kit in development.
Comments
Post a Comment