2017-01-09 13 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

もう1つの動作が必要ですが、今はどうなっていますか? – Rawburner

関連する問題