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>

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

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.
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...
Can you explain what is reflexive relation?
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 doctrine-project.org/docs/orm/2.0/en/reference/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.