2010-12-17 8 views
1

私はリンクテーブルと多対多の関係を持っています。以下の(簡略化された)スキーマを参照 チュートリアル(http://www.symfony-project.org/doctrine/1_2/en/05-Data-Fixtures#chapter_05_many_to_many)に基づいて作成symfonyオブジェクトの代わりに多対多リレーションプロパティ

スキーマのインポート/ビルドが正しく、phpmyadminが正しい外部キーを示しています。 私はその後「locatie」モジュールのindexSuccessテンプレートで私が呼び出すことができるという印象の下だ:

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

しかし、それdoesntの仕事、$ oProductは、オブジェクトが、の各プロパティを表す文字列のように見えるdoesntのため、製品クラス。 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

-1

のSymfony花茎戦略はレンダリング時に、あなたがやりたいために、生の値を取得しようとする必要がありオブジェをラップします。ここに例があります:

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

あなたの問題を解決するための希望!

+1

ここでは絶対に必要ありません。 –

0

ようこそ、stackoverflow、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

歓迎ネイサンのおかげで、あなたのソリューションも動作しますが、Locatieから(refClassを使用して)getProducts()を直接呼び出す方が簡単です。答えをありがとう! – tomvo

関連する問題