2010-12-17 3 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; 

하지만 나던 일을, $ 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: 
    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를 놓쳤습니다. 나는이 모든 교리의 내용에 익숙하지 않지만 내 생각은 옳았다. 나는 나의 locatie 모델로부터 직접 제품을 가져올 수 있어야한다. Jeremy에게 감사드립니다! – tomvo

-1

심포니 풍경 전략은 렌더링 할 때, 당신은 당신이 원하는 일을하기 위해 원시 값을 얻기 위해 시도해야 오브제를 래핑합니다. 다음은 예입니다.

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

희망 사항은 문제를 해결하는 데 도움이됩니다.

+1

여기는 절대적으로 필요하지 않습니다. –

0

스택 오버플로에 오신 것을 환영합니다.

ORM이 중간 또는 "통과"클래스의 모델을 생성하므로 LocatieProduct이라는 추가 클래스가 있습니다. 이처럼 사용합니다 :

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

관련 개체에 액세스하는 방법을 배우는 가장 좋은 방법은 lib/model/doctrine/base/에서 생성 된 코드를 읽는 것입니다.

종종 편의를 위해 모델에 메소드를 추가합니다. 예를 들어, lib/model/doctrine/Locatie.class.php 당신은 함수가 예상보다 같은 일을 추가 할 수 있습니다 : 당신은 Locatie에 관계로서 정의 된 제품이없는

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

환영 네이선을 주셔서 감사합니다. 솔루션도 작동하지만 Locatie (refClass 사용)에서 직접 getProducts()를 호출하는 것이 더 쉽습니다. 답변 해주셔서 감사합니다! – tomvo