I
I
Ilya Klimov2011-03-11 11:55:00
symfony
Ilya Klimov, 2011-03-11 11:55:00

Creating an object of a descendant class in Doctrine by a known field type (column_aggregation)

There is a project on Symfony (1.4) + Doctrine (1.2)
The project has the following class architecture: AbstractProduct, AlphaProduct, BetaProduct and GammaProduct classes are inherited from it, using column_aggregation and the 'type' field to distinguish between classes.

In one of the controllers, I need to create a new object of a known type. Those. (for example) if type 1 - you need to create AlphaProduct, 2 - BetaProduct, etc. Those. create a child object of the desired type from a known type.
At the moment it looks like this:

$classname = 'AbstractProduct';

foreach (Doctrine::getTable('AbstractProduct')->getOption('subclasses') as $subclass) {
  $inheritanceMap = Doctrine::getTable($subclass)->getOption('inheritanceMap');
  if ($inheritanceMap["type"] == $type) {
    $classname = $subclass;
    break;
  }
}

$this->product = new $classname();


But I really don't like the idea of ​​scanning all subclasses in search of the right one. Doctrine 2 makes this task easier. But is it possible to somehow solve it in Doctrine 1?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zizop, 2011-03-11
@zizop

Do you have classes for these models? In theory, an abstract class should have something like:
AbstractProduct::getSubclasses(). Then it will not be necessary once again to climb into the database. I think in this case such hardcoding would be appropriate.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question