2011-11-04 44 views
0

我使用symfony 1.4.15和doctrine。我有类别和子类别:Symfony从类别和子类别中获取记录

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'); 

,但我不现在怎么弄来自所有类别子类别的所有产品....谢谢!

+0

在您的模式中是 - 如果您获得所有带有类别的产品,那么您也具有此类别中所有子类别的产品。如果不是,请添加简单的装置,你想要做什么? –

+0

我可以从类别中获取所有产品,但无法从类别的所有子类别获取所有产品。我需要一个查询。 – denys281

+0

请添加灯具和示例。你的查询应该是如此的工作。 –

回答

2

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

2)在当前的模型中,为什么Product有一个关系到两个SubcategoryCategoryCategory可由Subcategory确定。

如果您修复其中之一,实现您的查询会容易得多。

+0

** 2 ** - 开头是“该类别可以由子类别确定”,但客户表示他们希望以这种方式重制类别和子类别(他们在项目开始后表示生产),我们一直否认他们,但...(** 1 ** - 我没有时间重拍类别和子类别:-( – denys281