根据关系显示外国字段

时间:2011-06-19 21:53:45

标签: symfony1 symfony-1.4

我有以下架构:

 catalogue:
  actAs: [Timestampable, Sortable]
  columns:
    id:
      type: integer(2)
      primary: true
      autoincrement: true
    catalogue_name:
      type: varchar(255)
  relations:
    catalogue:
        class: Product
        refClass: ProductPrices
        local: catalogue_id
        foreign: product_id
ProductPrices:
  actAs: [Timestampable]
  columns:
    product_id: { type: integer(4)}
    catalogue_id: { type: integer(2) }
    price: { type:real }
  relations:
    Product:    { local: product_id, foreign: id }
    catalogue:  { local: catalogue_id, foreign: id }
  indexes:
    unique_price:  { fields: [catalogue_id, product_id], type: unique }
Product:
  actAs:             [Timestampable]
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    product_code:
      type: varchar(10)
    product_desc:
      type: varchar(255)
    created_at:
      type: timestamp
  relations:
    catalogue:
        class: catalogue
        refClass: ProductPrices
        local: product_id
        foreign: catalogue_id

现在这是Productcatalogue

之间的多人关系

我要做的是在HTML表格中显示每个目录及其产品。

我创建了一个主题和一个Doctrine查询:

public static function getPricingTable()
{
    $q = Doctrine_Query::create()
      ->from('ProductPrices p');

   return $q->execute();
}

现在,我唯一的问题是现在显示目录和产品,即

目录1

* Product 1     
* Product 2        
* Product 3

目录2

* Product 1

感谢所有帮助

1 个答案:

答案 0 :(得分:0)

如果要获取目录的所有产品,只需使用目录实例的 getProduct()方法即可。

例如,如果您想获得所有产品的所有目录:

$catalogues = Doctrine::getTable('catalogue')->findAll();

foreach($catalogues as $catalogue){
   //$catalogue is an instance
   echo $catalogue->getCatalogueName()

   //you've got all products from a catalogue instance    
   foreach($catalogue->getProduct() as $product){
       echo $product->getProductDesc();
   }
}