2012-02-03 99 views
2

我有從數據庫加載的複選框(我使用實體字段類型)的形式。複選框是區域和區域。我有以下數據庫模式:Symfony2自定義表格

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

問題是,我需要下列形式。怎麼做?與選項

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

回答

1

感謝this post我已經通過自定義渲染表單模板解決了這個問題。

0

的EntityType領域:

  • 多=真
  • 擴大=真
  • 屬性= '名'
  • 類= 'YourBundle:YourEntity'
  • query_builder ='function(EntityRepository $ er){return $ er-> createQueryBuilder('r') - >其中( 'r.parentId IS NOT NULL') - > ORDERBY( 'r.parentId', 'ASC') - > ORDERBY( 'r.name', 'ASC');}」
+0

這沒有幫助。結果將按'name'進行排序,但是我需要按** name **排序的** Region **和** Dictricts **組合的結果。 – kuboslav 2012-02-03 12:13:01

+0

你是否在你的實體(parentId)上定義了自反關係?無論如何,你不能用EntityType字段帶來的原生query_builder來做到這一點。你必須定義一個自反關係,用INDEX BY DQL查詢得到你的實體的良好排序,並將結果發送到* choices *選項中。 – AlterPHP 2012-02-03 13:36:01

+0

你能解釋什麼是_reflexive relation_? – kuboslav 2012-02-03 13:53:20