Doctrine inheritance. Is there an easy way to get all child classes/tables for using them in a select field in symfony forms?

StackOverflow https://stackoverflow.com/questions/2346742

Question

I just started to use symfony 1.4 and Doctrine. (Used 1.0 - 1.2 + Propel a lot before).

I thought to give Doctrine a try, because of the fast and huge Development process in the past.

Thanks to jwage ;-)

Im using Table Inheritance. This is a small portion of my 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 

Im now thinking of using a simple way to create a the right form.

The user should have to select fields at the top of the form (like you can see here : http://msm-esv.dyndns.org/frontend_dev.php/fahrzeuge/insert )

You should choose the "parent class" like Rennfahrzeuge or Karts and so on.

After that the user should choose the child class like Tourenwagen or Sonstige.

Then the page should reload and display the right form.

Is there any function in Doctrine to get the inheritated/child classes for displaying them in the second select field?

(e.g. Rennfahrzeuge has Tourenwagen,..,..., Sonstige and Karts has TonyKart,...,...,Sonstige)

After that i could create dynamically the assigned form class like:

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

or i have thought about just setting the right model in the parent form class.

What are your thoughts? I would really appreciate any suggestions and help :-)

Thanks a lot,

Marco

Was it helpful?

Solution

Are there direct functions such as a Doctrine getSubclasses or a PHP get_children_of? Not that I've come across, in Doctrine or PHP. The closest I know about is the PHP function is_subclass which you could use while iterating through all of your possible classes.

Do your subclasses vary significantly from your main model? I only see one column variation in your examples above but I'm guessing that was just for brevity.

I realize the appeal of the structure you're attempting but it also seems like it could be an incredible pain in the long run. Would it be possible to instead set up a separate VehicleCategory model that referenced itself to provide a nested structure and then your Vehicles could belong to those categories? This would have Rennfahrzeuge as a top level category and Tourenwagen and Sonstige would be categories with a parent of Rennfahrzeuge with Vehicles belonging to all three. If you didn't have too much field variation in your child models you could include all of the custom fields in the Vehicles model and only display/set them at the appropriate times.

Just some thoughts, hope that helps.

OTHER TIPS

If you need to find the subclasses of a Doctrine Record you could use
$yourSuperObject->getTable()->getOption('subclasses') or
Doctrine::getTable('SuperClass')->getOption('subclasses');

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top