1

我刚开始使用 symfony 1.4 和 Doctrine。(以前使用过 1.0 - 1.2 + Propel)。

我想尝试一下 Doctrine,因为过去的快速和巨大的开发过程。

感谢 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 或 Karts 等。

之后,用户应该选择像 Tourenwagen 或 Sonstige 这样的子类。

然后页面应该重新加载并显示正确的表单。

Doctrine 中是否有任何函数可以获取继承/子类以在第二个选择字段中显示它们?

(例如 Rennfahrzeuge 有 Tourenwagen,..,..., Sonstige 和 Karts 有 TonyKart,...,...,Sonstige)

之后,我可以动态创建分配的表单类,例如:

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

或者我考虑过在父表单类中设置正确的模型。

你怎么认为?我真的很感激任何建议和帮助:-)

非常感谢,

马可

4

2 回答 2

1

如果您需要查找 Doctrine Record 的子类,您可以使用
$yourSuperObject->getTable()->getOption('subclasses')
Doctrine::getTable('SuperClass')->getOption('subclasses');

于 2010-05-20T14:42:59.870 回答
0

是否有诸如 DoctrinegetSubclasses或 PHP之类的直接函数get_children_of?并不是说我在 Doctrine 或 PHP 中遇到过。我所知道的最接近的是 PHP 函数is_subclass,您可以在迭代所有可能的类时使用它。

您的子类与您的主要模型有很大不同吗?我在上面的示例中只看到一列变化,但我猜这只是为了简洁。

我意识到你正在尝试的结构的吸引力,但从长远来看,它似乎也可能是一个令人难以置信的痛苦。是否可以设置一个单独的VehicleCategory模型来引用自身以提供嵌套结构,然后您Vehicles就可以属于这些类别?这将使Rennfahrzeuge成为顶级类别,而TourenwagenSonstige将是Rennfahrzeuge的父级并Vehicles属于所有三个类别的类别。如果您的子模型中没有太多的字段变化,您可以在模型中包含所有自定义字段,Vehicles并仅在适当的时间显示/设置它们。

只是一些想法,希望对您有所帮助。

于 2010-03-03T22:51:25.053 回答