2013-03-28 4 views
0

symfony2에서 작성한 양식이 있습니다.Symfony2 양식 오류 - 왜?

$builder->add('purchaseOrder','text'); 
$builder->add('product', 'entity', array(
     'class' => 'WICProductBundle:Product', 
     'property' => 'name', 
     'query_builder' => function(EntityRepository $er) { 
      return $er->createQueryBuilder('p') 
       ->where('p.account=?1') 
       ->orderBy('p.name', 'ASC') 
       ->setParameter(1,$this->account); 
     } 
    )); 

이 양식을 Twig에서 출력하고 제출하면 문제가 없습니다.

양식 작성기를 변경하여 양식을 제출하면 오류가 발생합니다.

$builder->add('purchaseOrder','text'); 
$builder->add('product','text'); 

나는 8 구매 주문 필드에 입력하고 I 제품의 옵션은 모두 잘 작동합니다 ... (65)의 값을 드롭 다운 선택 형태의 첫 번째 반복 ...

내가 65의 값으로 제품 필드를 입력 ... 그리고이 오류 얻을 8로 구매 주문 필드를 입력 양식의 두 번째 반복 :

Catchable Fatal Error: Argument 1 passed to WIC\PurchaseOrderLineItemBundle\Entity\PurchaseOrderLineItem::setProduct() must be an instance of WIC\ProductBundle\Entity\Product, string given, called in /Applications/MAMP/htdocs/symfonydev/vendor/symfony/symfony/src/Symfony/Component/Form/Util/PropertyPath.php on line 538 and defined in /Applications/MAMP/htdocs/symfonydev/src/WIC/PurchaseOrderLineItemBundle/Entity/PurchaseOrderLineItem.php line 302 

를 왜하고자했다에서 내 양식 휴식 매번 동일한 값인 "65"가 전달되면 드롭 다운을 텍스트 상자로 바꿔 넣으시겠습니까?

+1

캐시를 지우셨습니까? –

+0

예, 캐시가 아닙니다. 나는 처음에 "query_builder"가 제품에 대한 객체를 생성했다는 사실과 관련이 있다고 생각합니다. 그런 다음 객체를 더 이상 존재하지 않는 간단한 텍스트 필드로 변경했습니다. 텍스트 필드 밖으로 개체를 만드는 방법을 모르거나 심지어 가능합니다. – LargeTuna

답변

1

symfony official documentation에서이 주제와 관련된 공식적인 설명을 찾을 수 없지만 AFAIK 관계를 나타내는 양식 필드는 Entity Field Type으로 렌더링되어야합니다. 이것은 symfony의 기본 폼 기능이 엔티티를 나타내는 폼의 필드를 렌더링하고 유지하는 유일한 방법입니다.

그럼에도 불구하고 Data Transformers을 사용하여 원하는 것을 정확히 얻을 수 있습니다. 텍스트 필드를 통해 엔티티를 사용하려면

Say you have a one-to-one relation of Task to Issue, e.g. a Task optionally has an issue linked to it. Adding a listbox with all possible issues can eventually lead to a really long listbox in which it is impossible to find something. You might want to add a textbox instead, where the user can simply enter the issue number.

2

, 당신은에 엔티티의 당신의 "텍스트"표현을 변환하여 필드에 사용자 정의 모델 데이터 변압기를 추가해야합니다 : 심포니 cookbook에서

"실제"엔티티 & 엔티티를 "텍스트"표현으로 역변환합니다.

이렇게하면 setter typehint로 인해 PHP에서 치명적인 오류가 발생하는 것을 피할 수 있습니다.