Symfony 4-在我的表单中使用服务(在query_builder中)

时间:2019-10-08 16:33:10

标签: php forms symfony service autowired

在我的symfony 4项目中,以我的形式

我想使用一项服务来完成选择。所以,我这样做了:

表格

public function buildForm(FormBuilderInterface $builder, array $options, GroupeValidateursService $groupeValidateursService)
{
    // $groupeValidateursService = $options['groupeValidateursService'];

    $builder
        ->add('defaultGroupeValidateurs', EntityType::class, [
            'class' => GroupeValidateurs::class,
            'label' => "Service par défaut",
            'placeholder' => "Sélectionnez un service",
            'query_builder' => function () use ($groupeValidateursService) {
                return $groupeValidateursService->getNotDefaultGroups();
            },
            'choice_label' => 'nom',
        ]);
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => ParametresAdmin::class,
        'groupeValidateursService' => GroupeValidateursService::class,
    ]);
}

控制器:

$formDeleteDefaultGroupe = $this->createForm(DeleteDefaultGroupeType::class, $parametresAdmin, [
            "groupeValidateursService" => $this->groupeValidateursService,
        ]);

如果我在服务函数中设置了转储模具,那没关系,我有一个对象数组,就像在存储库查询中一样。

但是我有这个错误:

  

类型为“ Doctrine \ ORM \ QueryBuilder”的预期参数,已给出“数组”

我认为从函数服务返回数组时会出现问题:

/**
 * Retourne tous les groupes sauf celui par défaut
 *
 * @return array
 */
public function getNotDefaultGroups()
{
    $groupes = $this->repoGroupeValidateurs->findAll();
    $default = $this->getDefaultGroupe();

    $otherGroups = [];

    foreach ($groupes as $groupe) {
        if ($groupe != $default) {
            $otherGroups[] = $groupe;
        }
    }

    return $otherGroups;
}

所以我不明白怎么了

编辑:我尝试过这样:

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

    $builder
        ->add('defaultGroupeValidateurs', ChoiceType::class, [
            'label' => "Service par défaut",
            'placeholder' => "Sélectionnez un service",
            'choices' => $options['groupeValidateursService']->getNotDefaultGroups(),
            'choice_label' => function ($choice, $key, $value) {
                return $value->getNom();
            }
        ]);
}

但是我有这个错误:

  

在字符串上调用成员函数getNom()

因此,我将$ value-> getNom()替换为$ choice-> getNom()。

但是现在我遇到了这个错误:

Argument 1 passed to Symfony\Component\Form\FormRenderer::renderBlock() must be an instance of Symfony\Component\Form\FormView, string given

$formDeleteDefaultGroupe = $this->createForm(DeleteDefaultGroupeType::class, $parametresAdmin, [
            "groupeValidateursService" => $this->groupeValidateursService
        ]);
        $formDeleteDefaultGroupe->handleRequest($request);

1 个答案:

答案 0 :(得分:1)

EntityType在参数中需要QueryBuilder。您正在为返回数组的选项'query_builder'使用匿名函数。您必须改为返回QueryBuilder:

use Doctrine\ORM\EntityRepository;

$builder
    ->add('defaultGroupeValidateurs', EntityType::class, [
        'class' => GroupeValidateurs::class,
        'label' => "Service par défaut",
        'placeholder' => "Sélectionnez un service",
        'query_builder' => function (EntityRepository $entityRepository) {
            return $entityRepository->createQueryBuilder('gv')
                ->where('gv.group != :defaultGroup')
                ->setParameters(['defaultGroup' => 'someDefaultGrupIdentifier']);
        },
        'choice_label' => 'nom',
    ]);

如果您真的想使用服务中的功能,请第二选择:使用ChoiceType,您可以在其中传递选择作为选项:

$builder->add('defaultGroupeValidateurs', ChoiceType::class, [
    'choices' => $options['groupeValidateursService']->getNotDefaultGroups(),
    'choice_label' => function ($choice, $key, $value) {
        return $value->getTitle()
    }
]);

更多信息:https://symfony.com/doc/current/reference/forms/types/entity.html#using-a-custom-query-for-the-entities

更多信息:https://symfony.com/doc/current/reference/forms/types/choice.html#example-usage