简体   繁体   English

在学说联接查询中获取表名

[英]Get table name in doctrine join query

I'm using Symfony 1.4 with Doctrine, and have this situation: 我将Symfony 1.4与Doctrine一起使用,并且遇到这种情况:

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. 我有一个表A,其中包含公共字段,另外3个表B,C和D都具有A的外键(a_id),以及每个表的额外字段。

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. 有了该数据(一个DoctrineCollection),我需要在我的视图中对其进行迭代以填充网格。 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). 问题是,我需要在该列表中指定记录的类型(即它属于表B,C还是D)。 How can I know from which table each field comes from? 我怎么知道每个字段来自哪个表?

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. 为此,您可以使用instanceofget_class进行适当操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM