Symfony从类别和子类别中获取记录

时间:2011-11-04 21:59:34

标签: symfony1 doctrine symfony-1.4

我使用symfony 1.4.15和doctrine。我有类别和子类别:

Category:
  actAs:
    Timestampable: ~
    Sluggable:
      unique: true
      canUpdate: true
      fields: [name]
      builder: [myTools, StripText]
    I18n:
      fields: [name]
  columns:
      name:   { type: string(255), notnull: true }

Subcategory:
  actAs:
    Timestampable: ~
    Sluggable:
      unique: true
      canUpdate: true
      fields: [name]
      builder: [myTools, StripText]
    I18n:
      fields: [name]
  columns:
    category_id:  { type: integer() }
    name:   { type: string(255), notnull: true }
  relations:
    Category: { onDelete: CASCADE,local: category_id , foreign: id }

我有产品。产品与类别和子类别有关系。

Product:
  actAs:
    Timestampable: ~
    Sluggable:
      unique: true
      canUpdate: true
      fields: [name]
      builder: [myTools, StripText]
    I18n:
      fields: [name,description,shortbody,meta_keywords,meta_description]
  columns:
    partner_id:           { type: integer() }
    active:               { type: boolean, default: 0, notnull: false }
    name:                 { type: string(255), notnull: true }
    shortbody:            { type: string(500), notnull: true }
    description:          { type: string(), notnull: true }
    reference:            { type: string(100), notnull: true }
    code:                 { type: string(100), notnull: true }
    delivery_period:      { type: string(100), notnull: true }
    shipping_volume:      { type: string(100), notnull: true }    
    weight:               { type: string(100), notnull: true } 
    packing:              { type: string(100), notnull: true } 
    package_dimensions:   { type: string(100), notnull: true } 
    type_of_packaging:    { type: string(100), notnull: true } 
    video_url:            { type: string(100), notnull: false }
    meta_keywords:        { type: string(255) }
    meta_description:     { type: string(255) }
  relations:
    Subcategory:          { local: product_id , foreign: subcategory_id, refClass: ProductSubcategory } 
    Category:             { local: product_id , foreign: category_id, refClass: ProductCategory } 
    Partner:              { local: partner_id , foreign: id, onDelete: CASCADE }



ProductSubcategory:
  connection: doctrine
  columns:
    subcategory_id:  { type: integer(), primary: true}
    product_id:      { type: integer(), primary: true }
  relations:
    Product:         {  onDelete: CASCADE,local: product_id, foreign: id }
    Subcategory:     {  onDelete: CASCADE,local: subcategory_id, foreign: id }

ProductCategory:
  connection: doctrine
  columns:
    category_id:  { type: integer(), primary: true}
    product_id:   { type: integer(), primary: true }
  relations:
    Product:             {  onDelete: CASCADE,local: product_id, foreign: id }
    Category:            {  onDelete: CASCADE,local: category_id, foreign: id }

所以我需要从子类别和类别中获取所有产品(一个查询) 我可以获得属于类别的所有产品:

 $q = $this->createQuery('a')
                       ->andWhere('a.active=1')
                       ->leftJoin('a.ProductCategory o')
                       ->andWhere('o.Category_id=?',$category_id)
                       ->addORDERBY ('created_at DESC');

但我现在不知道如何从类别的所有子类别中获取所有产品....谢谢!

1 个答案:

答案 0 :(得分:2)

1)你知道Doctrine中的NestedSet行为吗?这应该可以解决您对Subcategory表的需求。它还允许“更深层次的类别”

2)在您当前的模型中,为什么 ProductSubcategoryCategory有关系? Category可以由Subcategory确定。

如果你修复其中一个,那么实现你的查询将会容易得多。