->add('healthPlan', EntityType::class, [ 'class' => Organization::class, 'choice_label' => function ($value, $key) { return 1; }, 'query_builder' => function (EntityRepository $er) { return $er->findAll(); } ]) 

Writes that wants query and there, as though, an array, but basically I need OrganizationRepository

Just do not offer to write a query via query_builder, since it is the function call that is needed.

  • First, provide the code for your OrganizationRepository - AmsTaFFix

2 answers 2

Use the choices option for example

 ->add( 'healthPlan', EntityType::class, [ 'class' => Organization::class, 'choice_label' => function ($value, $key) { return 1; }, 'choices' => function () use ($organizationRepository) { return $organizationRepository->customMethod(); } ] ) 

    I want to share my approach.

    In my practice, I often come across a situation where either a QueryBuilder is required, or a result is already built. In my opinion, it is more convenient to have 2 methods in the repository:

    getBySomeCriteriaQueryBuilder () - will return QueryBuilder

    getBySomeCriteria () - will return the result of the sample, for example:

     public function getBySomeCriteria() { return $this->getBySomeCriteriaQueryBuilder()->getQuery()->getResult(); } 

    Specifically, in your case, the convenience is that you do not need to throw anything extra into the class of the form. You have already transferred the repository to the anonymous function - only the method call needs to be inserted:

      ->add('healthPlan', EntityType::class, [ 'class' => Organization::class, 'choice_label' => function ($value, $key) { return 1; }, 'query_builder' => function (EntityRepository $er) { return $er->getBySomeCriteriaQueryBuilder(); } ]) 

    Also, in the future, based on this QueryBuilder, it will be possible to build other, more complex conditions, which will eliminate the duplication of code.