2010-02-27 149 views
1

我剛開始使用symfony 1.4和Doctrine。 (使用1.0 - 1.2 +之前很多推進)。主義繼承。有沒有一種簡單的方法來獲得所有的孩子類/表在symfony表單的選擇字段中使用它們?

我以爲給了主義一個嘗試,因爲過去的發展過程很快而且巨大。

感謝jwage ;-)

我使用表繼承。這是我的schema.yml的一小部分:

Articles: 
    columns: 
id: 
    type: integer(4) 
    primary: true 
    notnull: true 
    autoincrement: true 
marken_id: 
    type: integer(4) 
    notnull: false 
user_id: 
    type: integer(4) 
    notnull: false 
address_id: 
    type: integer(4) 
    notnull: false 

...

Vehicles: 

inheritance: 
    extends: Articles 
    type: concrete 

Rennfahrzeuge: 
columns: 
    stvo: 
    type: boolean 
    notnull: false 
    default: false 
inheritance: 
    extends: Vehicles 
    type: concrete 


Tourenwagen: 
    inheritance: 
    extends: Rennfahrzeuge 
    type: column_aggregation 
    keyField: type 
    keyValue: 1 

...

Sonstige: 
    inheritance: 
    extends: Rennfahrzeuge 
    type: column_aggregation 
    keyField: type 
    keyValue: 6 

Karts: 
    inheritance: 
    extends: Vehicles 
    type: concrete 
TonyKart: 
    inheritance: 
    extends: Karts 
    type: column_aggregation 
    keyField: type 
    keyValue: 1 

...

Sonstige: 
    inheritance: 
    extends: Karts 
    type: column_aggregation 
    keyField: type 
    keyValue: 9 

現在林想用一種簡單的方法來創建一個正確的形式。

用戶應在表格頂部的選擇字段(如你 可以在這裏看到:http://msm-esv.dyndns.org/frontend_dev.php/fahrzeuge/insert

你應該等選擇「父類」像Rennfahrzeuge或卡丁車和 。

之後,用戶應該選擇像Tourenwagen或 Sonstige這樣的孩子。

然後頁面應該重新加載並顯示正確的表單。

在Doctrine中是否有任何函數獲得繼承/子類 以在第二個選擇字段中顯示它們?

(如Rennfahrzeuge有Tourenwagen,...,...,SONSTIGE和卡丁車有 TonyKart,...,...,SONSTIGE)

之後,我可以動態地創建指定的表單類,如:

$chooseMode  = $request->getParameter('chooseMode').'Form'; 
$modeFormClass = new $chooseMode(); 

或我曾想過只設置正確的模式在父 窗體類。

你的想法是什麼?我會很感激的任何建議和幫助 :-)

非常感謝,

馬爾科

回答

0

是否有直接的功能,如學說getSubclasses或PHP get_children_of?不是我在Doctrine或PHP中遇到過。我所知道的最接近的是PHP函數is_subclass,您可以在遍歷所有可能的類時使用它。

你的子類與你的主模型有很大的不同嗎?我只在上面的例子中看到一列變化,但我猜這只是爲了簡潔。

我意識到你正在嘗試的結構的吸引力,但從長遠來看,它似乎也可能是一個令人難以置信的痛苦。是否有可能建立一個單獨的VehicleCategory模型來引用它自己以提供嵌套結構,然後您的Vehicles可能屬於這些類別?這將有Rennfahrzeuge爲頂級類別和TourenwagenSONSTIGE將類別與屬於所有三個Rennfahrzeuge父母與Vehicles。如果您的孩子模型中沒有太多的字段變化,您可以在Vehicles模型中包含所有自定義字段,並且只在適當的時間顯示/設置它們。

只是有些想法,希望有所幫助。

1

如果你需要找到一個學說記錄的子類,你可以使用
$yourSuperObject->getTable()->getOption('subclasses')
Doctrine::getTable('SuperClass')->getOption('subclasses');

相關問題