2011-11-04 46 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