폼에 드롭 다운 메뉴가 있으며 폼 구조는 값에 따라 다릅니다. 나는 이벤트 구독자/수신기 클래스로 "form-update-issue"를 해결할 수 있었다. 여기서 드롭 다운의 값에 따라 메인 폼을 업데이트하려고한다.Symfony2 임베디드 폼 + 동적 폼 업데이트
주된 문제는 데이터베이스에서 유지 된 값에서 양식을 수정해야한다는 것입니다. 내 DB 스키마 :
내가 가진 4 테이블 : 모델, ModelCategory, ModelCategoryKey, ModelParameter. 폼에서의 ModelCategory를 선택한 후 사용자 N ModelParameter
-
- ModelCategory 1 - N 모델 1 - m의 ModelParameter
- ModelCategory 1 - N ModelCategoryKey
- ModelCategoryKey 1 (모델 엔터티 기반 양식) 드롭 다운 내가 ModelParamater 행을 사용하여 폼을 업데이트해야하지만 번호와 기본값은 ModelCategory 1 - n ModelCategoryKey assocaiton에 따라 다릅니다.
PRE_BIND 이벤트 중에 기본 Model 엔티티에 새로운 ModelParameter 엔티티를 첨부하려고 시도했으며 (기본값도 설정 됨) 잘 동작하는 것처럼 보였지만 'collection'유형 지정 요소와 함께 'parameters'를 추가하면 양식에 다음 오류가 표시됩니다.
Entities passed to the choice field must be managed. Maybe persist them in the entity manager?
분명히 현재 내 엔티티를 유지할 수 없으며 유지하지 않아도됩니다.
모든 아이디어는 환영합니다!
UPDATE : 후 양식을 수정 preSubmit/preBind :
$form->add('parameters','collection',array( 'type' => new ModelParameterType(), ));
OR을 '공장'속성이 FormFactoryInterface입니다
. 오류 메시지는 동일합니다.$form->add( $this->factory->createNamed('parameters','collection',null, array( 'type' => new ModelParameterType() )) );
UPDATE2는 :
추가 조사는 assocation에 그 내가 추가하지 않는 경우 "기본"기관, 증명했다. 그러면 오류없이 작동합니다. 여기
내 폼 수정 방법의 원인 :public function preSubmit(FormEvent $event) { $form = $event->getForm(); $id = $event->getData()['modelCategory']; $entity = $form->getData(); $categoryKeys = $this->em->getRepository('MyBundle:ModelCategoryKey')->findByModelCategory( $this->em->getReference('MyBundle:modelCategory',$id) ); foreach ($categoryKeys as $key) { $param = new ModelParameter(); $param->setModel($entity); $param->setKey($key); $entity->addParameter($param); } $form->add( $this->factory->createNamed('parameters','collection',null, array( 'type' => new ModelParameterType(), 'allow_add' => true, 'cascade_validation' => true )) ); }
가 난 그냥 $ param->로 setModel ($ 엔티티)를 주석 한 해결하고자하는 것; 줄을 잘 작동하는 것 같습니다. 나는 이것이 실제로 작동한다면, 이것을 더 많이 연구하고 경험을 공유 할 것입니다.
선택 입력 필드가 아닌 새로 생성 된 엔티티를 콜렉션으로 표시하려고합니다. 오류 메시지가 선택 필드임을 알 수없는 이유는 무엇입니까? :) – nothingam