1
votes

I'm using Symfony 1.4 with Doctrine, and have this situation:

I have a table A that holds common fields, and 3 more tables B, C and D which all have a foreign key to A (a_id), and extra fields specific to each one.

In my model, I'm writing the following query:

 public function getAllFields(){
    $query = Doctrine_Core::getTable('A')
    ->createQuery('a')
    ->leftJoin('a.B b')
    ->leftJoin('a.C c')
    ->leftJoin('a.D d');        
    $result = $query->execute();                
    return $result;

Having that data (a DoctrineCollection), I need to iterate it in my view to fill a grid. The thing is that I need to specify in that list, the type of the record (that'd be whether it belongs to the table B, C, or D). How can I know from which table each field comes from?

1

1 Answers

2
votes

Well normally the records will be hydrated as the appropriate model class, so you wont have a flat result set. You then access the properties from the record with an accessor/array notation/object notation.

Example:

foreach($result as $record) {
   echo $a->getSomeAProperty();

   $b = $a->getB();
   echo $b->someBProperty();

   $c = $a->getC();
   echo $c->getSomeCProperty();

}

To get a table name from a model you can do:

$theModel->getTable()->getTableName();

Likewise if you use standard array hydration you will get back a nested array structure like:

array(
   'some_a_property' => 'value',
   'C' = > array(
      'some_c_property' => 'value'
   )
   // etc..
)

So you could determine what model/table the property comes from by the key for the nested array.

That said if you are doing things correctly table name should be unimportant, instead you should be concerned with what type of object it is. Table names can change and are merely a storage construct you probably don't want your logic at this level to be concerned with.

For that you can use instanceof and get_class to act appropriately.