2010-12-17 73 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