2017-12-21 40 views
0

doctrine 쿼리에서 배열을 가져오고 싶습니다. I했습니다 자체 참조 엔티티symfony doctrine 결과를 배열하는 QueryBuilder

앱 \ 엔티티 \의 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(); 
} 

이 저장소

앱 \ 법인 \ AccessModuleRepository

public function findAllArray() 
{ 
    return $this->createQueryBuilder('a') 
     ->getQuery() 
     ->getResult(); 
} 

이 반환 colection입니다에서 자녀와 부모가있는 개체의 하지만이 쿼리를 배열 ->getArrayResult()으로 가져 오려고 할 때 하위 배열 인 "children"과 "parent"없이 배열에 "id"와 "name"만 있습니다.

return $ 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" 
    ] 
] 

반환 $ this-> createQueryBuilder ('A') -> getQuery() -> getResult를();

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 ▶} 
] 

어떻게 객체를 referncing와 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' => [] 
    ] 
] 
+0

귀하의 질문은 명확하지 않다. 반환 할 것으로 예상되는 것은 무엇입니까? getResult가 참조하는 객체가있는 배열을 반환하기 때문입니다. 예상 결과를 추가 할 수 있습니까? – albert

+0

확인. 따라서 결과를 객체로 가져오고 싶지는 않지만 객체가있는 구조와 정확히 같은 구조를 가진 배열로 가져 오길 원하십니까? – albert

+0

예 albert, exacly. 객체가 가지고있는 배열의 sam 구조체를 원합니다. – nicram

답변

0

당신은 선택적 인수로 getResult를 메서드를 호출 할 수 있습니다 :

return $this->createQueryBuilder('a') 
    ->getQuery() 
    ->getResult(Query::HYDRATE_ARRAY); 
+0

이 배열은 배열이며 엔터티에서 자식 및 부모를 추가하지 않습니다. 'getArrayResult()'는 getResult (Query :: HYDRATE_ARRAY)의 별명입니다. – nicram

0

교리 어레이 계층 구조

당신이 중첩 된 세트의 계층 구조를 수화하고자하는 경우 객체 대신 배열에 넣으려면를 사용하면됩니다.상수. 객체 대신 PHP 배열을 사용한다는 점을 제외하고는 HYDRATE_RECORD_HIERARCHY과 동일합니다.

http://doctrine.readthedocs.io/en/latest/en/manual/data-hydrators.html#nested-set-array-hierarchy

public function findAllArray() 
{ 
    return $this->createQueryBuilder('a') 
     ->getQuery() 
     ->execute(array(), Doctrine_Core::HYDRATE_ARRAY_HIERARCHY);; 
} 
+0

Doctrine_Core 클래스가 없습니다. – nicram

+0

'stof/doctrine-extensions-bundle'을 설치했지만 Doctrine_Core 클래스는 아직 설치하지 않았습니다 :/어떤 번들이 존재합니까? – nicram

+1

'Doctrine_Core'는 Doctrine 1.x처럼 보입니다. 질문의 경우가 아닙니다. – dlondero