2010-07-08 47 views
0

我有什麼應該是與Symfony的一個簡單的問題。Symfony一對多嵌入顯示多種形式

在這個例子中,我有一個User,LookingFor和LookingForNames表。用戶表保存用戶帳戶並與LookingFor關聯。 LookingFor表格包含用戶和LookingForNames之間的任何關係。

示例:用戶'鏈'可能在LookingFor表中有兩個條目用於約會和談話,這些條目是基於來自LookingFor和LookingForNames的type_id從LookingForNames表查找的。

我遇到的問題是當我在用戶表單中查找嵌入關係時。它顯示兩次LookingFor表單,因爲用戶已選擇他們正在尋找約會和交談。如果我爲該用戶選擇了更多選項,則會顯示更多次。

例如。問題

LookingFor Form - Instance #1 
Dating - Checked 
Talk - Not Checked 
Friends - Not Checked 

LookingFor Form - Instance #2 
Dating - Not Checked 
Talk - Checked 
Friends - Not Checked 

的溶液將被示出LookingFor表一次在一個複選框格式,其中用戶的選擇將被預選。

例如。溶液

LookingFor Form - Only One Instance 
Dating - Checked 
Talk - Checked 
Friends - Not Checked 

的schema.yml

LookingFor: 
    connection: doctrine 
    tableName: looking_for 
    columns: 
    type_id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: false 
    uid: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: false 
    relations: 
    LookingForNames: 
     local: type_id 
     foreign: type_id 
     type: many 
LookingForNames: 
    connection: doctrine 
    tableName: looking_for_names 
    columns: 
    type_id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: true 
    name: 
     type: string(255) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    relations: 
    LookingFor: 
     local: type_id 
     foreign: type_id 
     type: many 
User: 
    connection: doctrine 
    tableName: user 
    columns: 
    id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: true 
    email: 
     type: string(255) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    gender: 
     type: string(6) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    age: 
     type: date(25) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    city: 
     type: string(255) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    state: 
     type: string(255) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    country: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
    profilepic: 
     type: string(255) 
     fixed: false 
     unsigned: false 
     primary: false 
     default: profileblank.jpg 
     notnull: false 
     autoincrement: false 
    relations: 
    LookingFor: 
     local: id 
     foreign: uid 
     type: many 
     foreignType: many 

UserEditForm

class UserEditForm extends BaseUserForm 
{ 
    public function configure() 
    { 
      $this->embedRelation('LookingFor'); 
    } 
} 

LookingForForm

的3210
class LookingForForm extends BaseLookingForForm 
{ 
    public function configure() 
    { 
     $this->useFields(array('type_id')); 
     $this->widgetSchema['type_id'] = new sfWidgetFormChoice(array(
      'choices' => Doctrine_Core::getTable('LookingForNames')->getFormChoiceNames(), 
      'expanded' => true, 
      'multiple' => true 
     )); 
    } 
} 

回答

1

LookingForForNames確實有必要嗎?看起來您正試圖增加對以後創建新的LookingFor類別的支持,例如食物愛好者,小帽子的人等

如果你真的認爲你會在後來添加很多這些LookingForNames,那麼這是有道理的,但如果你正在做一個約會/社交網站,我可以'不要以爲你會經常這樣做。如果您不需要頻繁添加類別,請嘗試使用這樣的模式:

LookingFor: 
    uid: 
    integer 
    dating: 
    integer 
    friends: 
    integer 
    small_hats: 
    integer 
    ... 

這樣,LookingFor甚至可以與用戶進行一對一的關聯。

0

看看sfGuard架構,因爲它可以做同樣的事情,但會將用戶與羣組關聯。

0

這可能是您的模式的問題。用戶不應該與LookingFor有關係,用戶應該與LookingForNames有許多關係。尋找作爲refClass。

如果您不想更改架構,也可以通過手動嵌入表格來修復此問題。看看嵌入關係在內部做了什麼。