nicram
nicram

Reputation: 373

symfony doctrine QueryBuilder to array result

I want to get array from doctrine query. I've self-referencing entity

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();
}

In this repository

App\Entity\AccessModuleRepository

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

this return colection of objects with children and parent. But when I want to get this query as array ->getArrayResult() I get array only with "id" and "name" without subarry "children" and "parent".

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

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

How to get array from getResult() with referncing objects?

Expected result

[
 [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' => []
    ]
]

Upvotes: 2

Views: 19245

Answers (2)

albert
albert

Reputation: 4468

Doctrine Array hierarchy

If you wish to hydrate the nested set hierarchy into arrays instead of objects you can do so using the HYDRATE_ARRAY_HIERARCHY constant. It is identical to the HYDRATE_RECORD_HIERARCHY except that it uses PHP arrays instead of objects.

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);;
}

Upvotes: 0

amirmodi
amirmodi

Reputation: 312

You can call getResult method with an optional argument:

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

Upvotes: 6

Related Questions