这是一个cakephp 3.1查询结果,我想直接访问$ data-> items但是我得到一个错误,说找不到关键项,我不想使用funtcion toArray(),因为如果我这样做的话将仅返回'items'=> ...并且我丢失了所有'query'=> ...信息.你能用正确的语法来帮助我获得$ data-> items或$ data - > {'items'} ....
$data = object(Cake\ORM\ResultSet) {
'query' => object(Cake\ORM\Query) {
'(help)' => 'This is a Query object, to get the results execute or iterate it.',
'sql' => 'SELECT Posts_types.id AS `Posts_types__id`, Posts_types.name AS `Posts_types__name` FROM posts_types Posts_types ORDER BY 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'
}
]
}
Run Code Online (Sandbox Code Playgroud)
问题中的项目键是调用的结果toArray
,这是您所问的问题的直接答案.
显而易见,查看结果集类的公共方法以及它使用的集合特征,结果集的任何属性都不能直接访问.
这是一个演示如何使用结果集的简单示例:
use Cake\ORM\TableRegistry;
$resultSet = TableRegistry::get('Posts');
// Iterate it
foreach($resultSet as $postObject) {
echo $postObject->title;
}
// Obtain data as an array
$arrayofPostObjects = $resultSet->toArray();
echo get_class($resultSet); // It is still a result set
Run Code Online (Sandbox Code Playgroud)
请注意,在resultSet上调用方法不会改变它的含义 - 它仍然是结果集,不会丢失信息.这个问题存在误解,但由于目前尚不清楚你在做什么,所以不清楚这种误解是什么.
在评论中你问过:
如果我想得到$ data-> query我该怎么办?
这是您的应用程序永远不应该做的事情,作为开发人员,您通常只想将其作为调试的一部分.大多数类实现__debugInfo - 这是调试/ var_dumping结果集对象时两个键"query"和"items"的来源:
/**
* Returns an array that can be used to describe the internal state of this
* object.
*
* @return array
*/
public function __debugInfo()
{
return [
'query' => $this->_query,
'items' => $this->toArray(),
];
}
Run Code Online (Sandbox Code Playgroud)
因此,要获得查询,您唯一需要做的就是调试/ var_dump对象.如前所述,这也表明"项目"只是调用的结果toArray
.
请注意,要记录所有查询配置查询日志记录,显然所有开发人员都应该在开发中使用Debug Kit.
归档时间: |
|
查看次数: |
5273 次 |
最近记录: |