2

I have form with checkboxes loaded from database (I use entity field type). Checkboxes are regions and districts. I have following database schema:

+-----------------------------------+
| id | parent_id | name             |
+-----------------------------------+
| 1  | NULL      | Region           |
+-----------------------------------+
| 2  | 1         | District         |
+-----------------------------------+
| 3  | 1         | Another district |
+-----------------------------------+
| 4  | NULL      | Another region   |
+-----------------------------------+
| 5  | 4         | Next district    |
+-----------------------------------+

Problem is that I need following form. How to do that?

<b>Region</b><!-- Loaded from database -->
<!-- Dictricts ordered by name -->
<input type="checkbox" id="someId" value="3"><label for="someId">Another district</label>
<input type="checkbox" id="someId" value="2"><label for="someId">District</label>
<b>Another region</b><!-- Loaded from database -->
<!-- Dictricts ordered by name -->
<input type="checkbox" id="someId" value="5"><label for="someId">Next district</label>
kuboslav
  • 1,430
  • 5
  • 16
  • 31

2 Answers2

1

Thanks to this post I've solve this by custom rendering form template.

Community
  • 1
  • 1
kuboslav
  • 1,430
  • 5
  • 16
  • 31
0

EntityType field with options :

  • multiple = true
  • expanded = true
  • property = 'name'
  • class = 'YourBundle:YourEntity'
  • query_builder = 'function (EntityRepository $er) {return $er->createQueryBuilder('r') ->where('r.parentId IS NOT NULL') ->orderBy('r.parentId', 'ASC')->orderBy('r.name', 'ASC');}'
AlterPHP
  • 12,667
  • 5
  • 49
  • 54
  • This will not help. Result will be sorted by `name` but I need result which will be grouped by **Region** and **Dictricts** in each region sorted by `name`. – kuboslav Feb 03 '12 at 12:13
  • Do you define a reflexive relation on your entity (on parentId) ? Anyway, you can't do that with the native query_builder brought with EntityType field. You'll have to define a reflexive relation, get your entities well-sorted with INDEX BY DQL query, and send the results in the *choices* option... – AlterPHP Feb 03 '12 at 13:36
  • Can you explain what is _reflexive relation_? – kuboslav Feb 03 '12 at 13:53
  • In this case, your Territory Entity is related on itself through the parentId property. Something like that with Annotation mapping : @ManyToOne(targetEntity="Territory") ; @JoinColumn(name="parent_id", referencedColumnName="id") on the parentId property. See http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html#many-to-one-unidirectional – AlterPHP Feb 03 '12 at 14:29