如何在 CakePHP 3.x 中对查询生成器结果使用 Hash?
How to use Hash on the query builder results in CakePHP 3.x?
这是我的代码
$all = $this->find()
->select(['ProductCircles.id', 'ProductCircles.type', 'ProductCircles.name'])
->order(['ProductCircles.type'])
->all()
->toArray();
Log::write('error', $all);
$all = Hash::combine($all, '{n}.ProductCircles.id', '{n}.ProductCircles.name', '{n}.ProductCircles.type');
$all
是 ProductCircle 实体数组。
但是,Hash combine 无法像我预期的那样对数据进行操作。
请指教
$all
应为 ProductCircle 实体数组:
2015-03-15 08:04:36 Error: Array
(
[0] => App\Model\Entity\ProductCircle Object
(
[_accessible:protected] => Array
(
[name] => 1
[type] => 1
[product_count] => 1
[products_in_circles] => 1
)
[_properties:protected] => Array
(
[id] => 27
[type] => MATERIAL
[name] => Wood
)
[_original:protected] => Array
(
)
[_hidden:protected] => Array
(
)
[_virtual:protected] => Array
(
)
[_className:protected] => App\Model\Entity\ProductCircle
[_dirty:protected] => Array
(
)
[_new:protected] =>
[_errors:protected] => Array
(
)
[_registryAlias:protected] => ProductCircles
)
这是我所期望的。
我想用 Hash::combine 做的是得到这样的数组:
$result:
[
[MATERIAL] => [
[27] => [
Wood
]
]
不使用Hash,而是查询对象内置的集合方法:
$combined = $this->find()
->select(['ProductCircles.id', 'ProductCircles.type', 'ProductCircles.name'])
->order(['ProductCircles.type'])
->combine('id', 'name', 'type')
->toArray();
扩展 José 的回答(并使用 extract()
因为那是我需要的):
$query = $this->MyTable->find('all', [
'contain' => [
'ProductCircles',
],
// ...
]);
return $query->all()->extract('ProductCircles.name');
更多信息:book.cakephp.org/3/en/core-libraries/collections.html#Cake\Collection\Collection::extract
这是我的代码
$all = $this->find()
->select(['ProductCircles.id', 'ProductCircles.type', 'ProductCircles.name'])
->order(['ProductCircles.type'])
->all()
->toArray();
Log::write('error', $all);
$all = Hash::combine($all, '{n}.ProductCircles.id', '{n}.ProductCircles.name', '{n}.ProductCircles.type');
$all
是 ProductCircle 实体数组。
但是,Hash combine 无法像我预期的那样对数据进行操作。
请指教
$all
应为 ProductCircle 实体数组:
2015-03-15 08:04:36 Error: Array
(
[0] => App\Model\Entity\ProductCircle Object
(
[_accessible:protected] => Array
(
[name] => 1
[type] => 1
[product_count] => 1
[products_in_circles] => 1
)
[_properties:protected] => Array
(
[id] => 27
[type] => MATERIAL
[name] => Wood
)
[_original:protected] => Array
(
)
[_hidden:protected] => Array
(
)
[_virtual:protected] => Array
(
)
[_className:protected] => App\Model\Entity\ProductCircle
[_dirty:protected] => Array
(
)
[_new:protected] =>
[_errors:protected] => Array
(
)
[_registryAlias:protected] => ProductCircles
)
这是我所期望的。
我想用 Hash::combine 做的是得到这样的数组:
$result:
[
[MATERIAL] => [
[27] => [
Wood
]
]
不使用Hash,而是查询对象内置的集合方法:
$combined = $this->find()
->select(['ProductCircles.id', 'ProductCircles.type', 'ProductCircles.name'])
->order(['ProductCircles.type'])
->combine('id', 'name', 'type')
->toArray();
扩展 José 的回答(并使用 extract()
因为那是我需要的):
$query = $this->MyTable->find('all', [
'contain' => [
'ProductCircles',
],
// ...
]);
return $query->all()->extract('ProductCircles.name');
更多信息:book.cakephp.org/3/en/core-libraries/collections.html#Cake\Collection\Collection::extract