2

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' => []
    ]
]
3
  • Your question it is not clear. What do you expect to be returned? Because getResult is returning an array with referencing objects. Can you add the expected result? Commented Dec 21, 2017 at 17:40
  • Ok. So you don't want to get the results as objects but you want to get them as arrays with exactly the same structure that has the object? Commented Dec 21, 2017 at 19:02
  • Yes albert, exacly. I want the sam structure in array as object has. Commented Dec 21, 2017 at 20:15

2 Answers 2

6

You can call getResult method with an optional argument:

return $this->createQueryBuilder('a')
    ->getQuery()
    ->getResult(Query::HYDRATE_ARRAY);
Sign up to request clarification or add additional context in comments.

1 Comment

This flats array and doesn't add children and parent from entity. getArrayResult() is alias of getResult(Query::HYDRATE_ARRAY)
0

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

5 Comments

I don't have Doctrine_Core class.
I've installed stof/doctrine-extensions-bundle but still no Doctrine_Core class :/ In which bundle it exists?
Doctrine_Core looks like Doctrine 1.x which is of course not the case of the question.
I've found part of solution ->setHint(\Doctrine\Orm\Query::HINT_INCLUDE_META_COLUMNS, true). This returns parent_id field with id of it's parent, but don't return array of children still. This Hint only ads foreign keys. But how to add array of childrens?
Hi, any idea how to add array of children?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.