Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
symfony条令查询生成器到数组结果_Symfony_Doctrine_Query Builder - Fatal编程技术网

symfony条令查询生成器到数组结果

symfony条令查询生成器到数组结果,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

我想从条令查询中获取数组。 我有自我参照实体

App\Entity\AccessModule

/**
 * @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
字段,但不返回子项数组。此提示仅显示外键。但是如何添加子项数组?嗨,知道如何添加子项数组吗?