symfony条令查询生成器到数组结果
我想从条令查询中获取数组。 我有自我参照实体 App\Entity\AccessModulesymfony条令查询生成器到数组结果,symfony,doctrine,query-builder,Symfony,Doctrine,Query Builder,我想从条令查询中获取数组。 我有自我参照实体 App\Entity\AccessModule /** * @ORM\ManyToOne(targetEntity="App\Entity\AccessModule", inversedBy="children") * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true) */ private $parent; /** * @ORM\OneTo
/**
* @ORM\ManyToOne(targetEntity="App\Entity\AccessModule", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true)
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="App\Entity\AccessModule", mappedBy="parent", fetch="EAGER")
* @Serializer\MaxDepth(2)
*/
private $children;
/**
* AccessModule constructor.
*/
public function __construct()
{
$this->children = new ArrayCollection();
}
在此存储库中
App\Entity\AccessModuleRepository
public function findAllArray()
{
return $this->createQueryBuilder('a')
->getQuery()
->getResult();
}
此返回包含子对象和父对象的对象集合。但是当我想以数组的形式获取这个查询时,->getArrayResult()
我只获取带有“id”和“name”的数组,没有子数组“children”和“parent”
返回$this->createQueryBuilder('a')->getQuery()->getArrayResult()强>
array:3 [▼
0 => array:2 [▼
"id" => 2
"name" => "Common module"
]
1 => array:2 [▼
"id" => 3
"name" => "User Module"
]
2 => array:2 [▼
"id" => 4
"name" => "Admin Module"
]
]
array:3 [▼
0 => AccessModule {#5118 ▼
-id: 2
-name: "Common module"
-parent: null
-children: PersistentCollection {#5208 ▼
-snapshot: array:2 [ …2]
-owner: AccessModule {#5118}
-association: array:15 [ …15]
-em: EntityManager {#2624 …11}
-backRefFieldName: "parent"
-typeClass: ClassMetadata {#3093 …}
-isDirty: false
#collection: ArrayCollection {#5209 ▼
-elements: array:2 [▼
0 => AccessModule {#5325 ▼
-id: 3
-name: "User Module"
-parent: AccessModule {#5118}
-children: PersistentCollection {#5327 ▶}
}
1 => AccessModule {#5328 ▼
-id: 4
-name: "Admin Module"
-parent: AccessModule {#5118}
-children: PersistentCollection {#5330 ▶}
}
]
}
#initialized: true
}
}
1 => AccessModule {#5325 ▶}
2 => AccessModule {#5328 ▶}
]
返回$this->createQueryBuilder('a')->getQuery()->getResult()强>
array:3 [▼
0 => array:2 [▼
"id" => 2
"name" => "Common module"
]
1 => array:2 [▼
"id" => 3
"name" => "User Module"
]
2 => array:2 [▼
"id" => 4
"name" => "Admin Module"
]
]
array:3 [▼
0 => AccessModule {#5118 ▼
-id: 2
-name: "Common module"
-parent: null
-children: PersistentCollection {#5208 ▼
-snapshot: array:2 [ …2]
-owner: AccessModule {#5118}
-association: array:15 [ …15]
-em: EntityManager {#2624 …11}
-backRefFieldName: "parent"
-typeClass: ClassMetadata {#3093 …}
-isDirty: false
#collection: ArrayCollection {#5209 ▼
-elements: array:2 [▼
0 => AccessModule {#5325 ▼
-id: 3
-name: "User Module"
-parent: AccessModule {#5118}
-children: PersistentCollection {#5327 ▶}
}
1 => AccessModule {#5328 ▼
-id: 4
-name: "Admin Module"
-parent: AccessModule {#5118}
-children: PersistentCollection {#5330 ▶}
}
]
}
#initialized: true
}
}
1 => AccessModule {#5325 ▶}
2 => AccessModule {#5328 ▶}
]
如何通过引用对象从getResult()获取数组
预期结果
[
[0] => [
'id' => 1,
'name' => 'Common Module',
'parent' => null,
'children => [
[0] => [
'id' => 2,
'name' => 'User Module',
'parent' => 1,
'children' => []
],
[1] => [
'id' => 3,
'name' => 'Admin Module',
'parent' => 1,
'children' => []
]
]
],
[1] => [
'id' => 2,
'name' => 'User Module',
'parent' => 1,
'children' => []
],
[2] => [
'id' => 3,
'name' => 'Admin Module',
'parent' => 1,
'children' => []
]
]
可以使用可选参数调用getResult方法:
return $this->createQueryBuilder('a')
->getQuery()
->getResult(Query::HYDRATE_ARRAY);
条令数组层次结构
public function findAllArray()
{
return $this->createQueryBuilder('a')
->getQuery()
->getResult();
}
如果希望将嵌套的集合层次结构分解为数组而不是对象,可以使用hydration\u ARRAY\u hierarchy
常量。它与hydroe\u RECORD\u层次结构
相同,只是它使用PHP数组而不是对象
你的问题不清楚。你希望得到什么回报?因为getResult正在返回一个包含引用对象的数组。你能添加预期的结果吗?好的。所以你不想得到作为对象的结果,但你想得到作为数组的结果,它们的结构与对象完全相同?是的,艾伯特,举个例子。我希望数组中的sam结构与对象相同。此平面数组不从实体添加子项和父项
getArrayResult()
是getResult(查询::水合物\u数组)的别名
我没有Doctrine\u Core类。我安装了stof/Doctrine extensions bundle
但仍然没有Doctrine\u Core类:/it存在于哪个bundle中?Doctrine\u Core
看起来像Doctrine 1.x,这当然不是问题所在。我找到了部分解决方案->setHint(\doctor\Orm\Query::HINT\u INCLUDE\u META\u COLUMNS,true)
。这将返回带有父项id的parent\u id
字段,但不返回子项数组。此提示仅显示外键。但是如何添加子项数组?嗨,知道如何添加子项数组吗?