2010-12-17 68 views
1

我有一個多對多的關係與鏈接表。請參閱下面的(簡化)架構。 根據教程創建(http://www.symfony-project.org/doctrine/1_2/en/05-Data-Fixtures#chapter_05_many_to_manysymfony多對多的關係循環屬性,而不是對象

架構導入/構建正確和phpmyadmin顯示外鍵正確。 我的印象是,事後在「locatie」模塊的indexSuccess模板我可以打電話:

foreach($locatie->getProducts() as $oProduct): 
    echo $oProduct->naam; 
endforeach; 

但是,這並不工作,因爲$ oProduct犯規似乎是一個對象,但表示在每個屬性的字符串產品類。 foreach只是簡單地循環第一個產品的屬性而不是產品列表。 有人有什麼建議嗎?


架構

Locatie: 
    connection: doctrine 
    tableName: locatie 
    columns: 
     locatie_id: 
     type: integer(4) 
     fixed: false 
     unsigned: true 
     primary: true 
     autoincrement: true 
     naam: 
     type: string(255) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: true 
     autoincrement: false 
LocatieProduct: 
    connection: doctrine 
    tableName: locatie_product 
    columns: 
    locatie_product_id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: true 
    locatie_id: 
     type: integer(4) 
     fixed: false 
     unsigned: true 
     primary: false 
     notnull: true 
     autoincrement: false 
    product_id: 
     type: integer(4) 
     fixed: false 
     unsigned: true 
     primary: false 
     notnull: true 
     autoincrement: false 
    relations: 
    Locatie: 
     local: locatie_id 
     foreign: locatie_id 
     foreignAlias: LocatieProducts 
     onDelete: CASCADE 
    Product: 
     local: product_id 
     foreign: product_id 
     foreignAlias: LocatieProducts 
     onDelete: CASCADE 
Product: 
    connection: doctrine 
    tableName: product 
    columns: 
    product_id: 
     type: integer(4) 
     fixed: false 
     unsigned: true 
     primary: true 
     autoincrement: true 
    naam: 
     type: string(255) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 

回答

1

您沒有定義爲Locatie關係的產品。將您的架構更改爲:

Locatie: 
    connection: doctrine 
    tableName: locatie #this isn't necssary, by the way 
    columns: 
    #etc 
    relations: 
    Products: 
     class: Product 
     type: many 
     refClass: LocatieProduct 
     local: locatie_id #the field on LocatieProduct that is an FK to the id of the current table (Locatie) 
     foreign: product_id #the field on LocatieProduct that is an FK to the id of the class (Product) 

另請注意,您不需要LocatieProduct上的字段locatie_product_id。如果你確實希望這個表有一個主鍵,我只需簡單地將它命名爲id。

Here's more from the Doctrine book

+0

這解決了這個問題,我錯過了很多和refClass。我對這個整個原則有些新東西,但我的想法是對的,我應該能夠直接從我的本地模型中檢索產品。謝謝傑里米! – tomvo 2010-12-18 09:37:26

-1

Symfony的景觀策略包裝OBJETS渲染時,你應該嘗試以你想要的東西,以獲得原始值。這裏是一個exmaple:

foreach($locatie->getRawValue()->getProducts() as $oProduct): 
    echo sfOutputEscaper::unescape($oProduct->naam); 
endforeach; 

希望能幫助你解決問題!

+1

這在這裏絕對沒有必要。 – 2010-12-17 19:17:13

0

歡迎堆棧溢出,tomvo。

ORM爲您的中級或「通過」類創建了一個模型,因此您有一個名爲LocatieProduct的額外類,您並不期待。你會使用這樣的:

foreach($locatie->getLocatieProducts()->getProduct() as $oProduct): 
    echo $oProduct->naam; 
endforeach; 

學習如何訪問相關的對象,最好的辦法是在lib/model/doctrine/base/閱讀生成的代碼。

我常添加其他方法到模型的便利。例如,在lib/model/doctrine/Locatie.class.php可以添加功能的工作更像是你期望:

public function getProducts() { 
    $a = array(); 
    foreach ($this->getLocatieProducts()->getProduct() as $p) { 
     $a[] = $p; 
    } 
    return $a; 
} 
+0

感謝Nathan的歡迎,您的解決方案也可以工作,但我發現您可以更輕鬆地直接從Locatie(使用refClass)調用getProducts()。感謝你的回答! – tomvo 2010-12-18 09:38:19