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.
OrganizationRepository- AmsTaFFix