2013-08-13 2 views
2

AttributeSet에서 여러 속성이있는 Product 양식을 만들려고합니다. 속성이 AttributeSet에 할당되고 제품 양식이로드되면 할당 된 모든 속성이 양식으로 렌더링됩니다. 각 속성은 유형 (텍스트, 선택 사항, 라디오 등 ...)Symfony2.3 두 entitnes의 동적 양식 필드

잘 모르겠습니다. 이것은 올바른 방법 인 경우 (난 그렇게 할 수있는 청소기/더 나은 방법이 확신),하지만 난 이런 식으로했다 :

우선의 주요 제품 형태의 유형

$builder->addEventSubscriber($this->buildProductAttributeFormListener) 

에 EventSubscriber 추가 preSetData 이벤트가있는 EventSubscriber :

이 그렇게 할 수있는 올바른 방법 인 경우

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    // this loop adds each attribute as a field with his type (choice, text, etc...) 
    foreach ($options['options'] as $i => $attribute) { 
     if (!$attribute instanceof AttributeInterface) { 
      throw new LogicException('Each object passed as attribute must implement "AttributeInterface"'); 
     } 

     $fieldOptions = array(
      'label' => $attribute->getName(), 
     ); 

     if ($attribute->getType() == 'choice') { 
      // add custom options for choice type 
      //$fieldOptions = array_merge($fieldOptions, array('mapped' => false)); 
     } 

     if (is_array($attribute->getOptions())) { 
      $fieldOptions = array_merge($fieldOptions, $attribute->getOptions()); 
     } 

     $builder->add($attribute->getId(), $attribute->getType(), $fieldOptions); 
    } 
} 

지금 나는이와 몇 가지 문제가 ... 1) 나는 확실하지 않다 다음 'rs_product_attribute_collection'유형의 내부

public function preSetData(FormEvent $event) 
{ 
    $product = $event->getData(); 
    $form = $event->getForm(); 

    if (null === $product->getAttributeSetId()) { 
     // need to get attribute set id from request 
     $request = Request::createFromGlobals()->get('rs_product_type_step'); 
     $attributeSetId = $request['attribute_set_id']; 
     if (!$attributeSetId) { 
      $request = Request::createFromGlobals()->get('rs_product'); 
      $attributeSetId = $request['attribute_set_id']; 
     } 
     // get assigned attribute set 
     $attributeSet = $this->asRepository->find($attributeSetId); 
    } else { 
     $attributeSet = $product->getAttributeSetId(); 
    } 

    // If product has attributes, lets add this configuration field. 
    if ($attributeSet->hasAttributes()) { 
     $form->add('attributes', 'rs_product_attribute_collection', array(
      'options' => $attributeSet->getAttributes(), 
      'required' => false 
     )); 
    } 
} 

,이 buildForm이 form-> bind가 데이터를 올바르게 바인드하지 않기 때문에 PRE_SUBMIT 이벤트를 추가하여 요청에서 속성 데이터를 가져 와서 속성을 Product에 수동으로 할당했습니다. 2) '선택'필드 유형에 대한 유효성 검사에서 "이 값은 유효하지 않습니다"라는 오류 메시지가 표시됩니다.

어떻게 작동시키는 지 아이디어를 듣고 싶습니다.

감사합니다. Ron.

답변