I have a self referencing table as covered in the doctrine documentation (which is already in heavy use throughout the application and therefore changing to nested tree isn’t a current option).
The table structure looks like this:
+----+----------+--------+
| id | parentID | Name |
+----+----------+--------+
| 1 | null | Name 1 |
| 2 | 1 | Name 2 |
| 3 | 1 | Name 3 |
| 4 | 2 | Name 4 |
| 5 | 1 | Name 5 |
--------------------------
Up until now in our forms we’ve been using the entire entities content our form, ie
$builder->add('hierarchyid', 'entity', array(
'class' => 'AcmeTestBundle:Hierarchies')
Which works fine, but now I want to be able to amend this to set points in the table, ie something like:
->add('hierarchyid', 'entity', array(
'class' => 'AcmeTestBundle:HierarchiesTest',
'query_builder' => function(HierarchiesTestRepository $repo)
{return $repo->findBy??????}
But I’ve hit a mental block how to complete this, any ideas?
One option I’ve considered is I already have code that builds an array collection of these entities, which is used elsewhere, but I’m unfamiliar with how I should go about implement a doctrine arraycollection in a form type.
private function createNodeArray($node)
{
$this->hierarchyArrayCollection->add($node);
foreach ($node->getChildren() as $hierarchy)
{
$this->createNodeArray($hierarchy);
}
}
For anyone interested the solution I went with in the end was to generate an array and use choice field type rather than entity,such as this: