2017-01-09 3 views
0

컨트롤러에서 양식 빌더로 객체를 전달하여 나중에 해당 객체를 ChoiceType 필드로 사용할 수 있습니다. 어떻게해야합니까? 이것은 내 SubAgentType.php컨트롤러에서 객체 유형 전달

class SubAgentType extends AbstractType { 

    protected $choices; 

    public function __construct (Choices $choices) 
    { 
     $this->choices = $choices; 
    }  

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 

     $builder->add('company_id', ChoiceType::class, array(
      'mapped' => false, 
      'choices' => $choices, 
     )); 

문제는 내가 아래의 오류입니다

$choices   = []; 
    $table2Repository = $this->getDoctrine()->getRepository('SwipeBundle:Company'); 
    $table2Objects = $table2Repository->findAll(); 

    foreach ($table2Objects as $table2Obj) { 
     $choices[$table2Obj->getId()] = $table2Obj->getId() . ' - ' . $table2Obj->getName(); 
    } 

    $form = $this->createForm(SubAgentType::class, $choices, array(
     'action'=>$this->generateUrl('backend_sub_agent_create'), 
     'method'=>'POST' 
    )); 

:

이 내 컨트롤러입니다. 당신의 MyFormType에서

myform.type: 
    class: AppBundle\Form\MyFormType 
    arguments: 
     - '@doctrine.orm.entity_manager' 
    tags: 
     - { name: form.type } 

: 당신의 services.yml 파일에

:

Catchable Fatal Error: Argument 1 passed to MyBundle\Form\SubAgentType::__construct() must be an instance of MyBundle\Form\Choices,

답변

0

귀하의 질문에 응답하려면 명확하게

/** 
* @var EntityManagerInterface 
*/ 
protected $em; 

/** 
* LicenseeType constructor. 
* 
* @param EntityManagerInterface $em 
*/ 
public function __construct(EntityManagerInterface $em) 
{ 
    $this->em = $em; 
} 

/** 
* @param FormBuilderInterface $builder 
* @param array    $options 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $choices   = []; 
    $table2Repository = $this-em->getRepository('SwipeBundle:Company'); 
    $table2Objects = $table2Repository->findAll(); 

    foreach ($table2Objects as $table2Obj) { 
     $choices[$table2Obj->getId()] = $table2Obj->getId() . ' - ' . $table2Obj->getName(); 
    } 

    ... 

, 당신은하지 않습니다 엔티티 관계를 올바르게 정의하는 경우이를 수행해야합니다.

+0

' "myform.type"에 대한 구성을로드 할 수있는 확장명이 없습니다. – phpmeter

0

봅니다 $ 선택은 선택의 서브 타입 일 필요는 없습니다, SubAgentType에 생성자를 해결하려면

public function __construct ($choices) 
{ 
    $this->choices = $choices; 
} 

그런 다음 생성자가 배열에 동의해야합니다, 컨트롤러에 배열을 삽입합니다.

+0

여전히 작동하지 않습니다. – phpmeter

+0

다른 동작이 있어야합니다. 지금은 무엇입니까? – Rawburner

관련 문제