这是一个 cakephp 3.1 查询结果,我想直接访问 $data->items 但我收到一条错误,指出未找到关键项,并且我不想使用函数 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'
}
]
}
最佳答案
没有 items 属性
问题中的 items 键是调用 toArray
的结果,这是对您所问问题的直接答案。
查看the result set class的公共(public)方法可能会很明显。 ,和the collection trait它使用的结果集的任何属性都不能直接访问。
这是一个演示如何使用结果集的简单示例:
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
请注意,在结果集上调用方法不会改变它的本质 - 它仍然是一个结果集,不会丢失信息。这个问题存在误解,但因为不清楚你在做什么,所以不清楚这个误解是什么。
获取查询怎么样?
在您提出的评论中:
if I want to get $data->query what should I do?
这是您的应用程序永远不应该做的事情,作为开发人员,您通常只想将其作为调试的一部分。大多数类实现 __debugInfo - 这就是 debug/var_dumping a result set object 时两个键“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(),
];
}
因此,要获取查询,您唯一需要做的就是调试/var_dump 对象。正如前面提到的,这也表明“items”只是调用 toArray
的结果。
请注意,要记录所有查询,请配置 query logging ,显然所有开发人员都应该使用 Debug Kit正在开发中。
关于php - 如何访问对象数据 php cakephp 3.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32696651/