Symfony 3 - 从传递给表单的实体对象获取选择选项?

Symfony 3 - Get choices option from entity object passed to the form?

我有一个 WorkflowType 表单 class,其中 data_class 属性是 Workflow::class。在控制器操作函数中创建表单时,我将一个 Workflow 对象 传递给它,它有一个 states 属性:

public function createWorkflowAction(Request $request, Workflow $workflow) {
    $secondFormPart = $this->createForm(WorkflowType::class, $workflow);
    ...
}

我在表单中添加了一个 EntityType 表单字段,其中 class 属性为 State::class。这个实体 class 有不止一个 属性 但我只使用其中一个并想将 EntityType 字段呈现为 select 框:

class WorkflowType extends AbstractType {

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {
        //@formatter:off
            $builder
                ->add('initialState', EntityType::class, array(
                    'class'             => State::class,
                    'choice_label'      => 'key',
                    'choice_value'      => 'key',

                    // This combination of 'expanded' and 'multiple' implements a select box
                    'expanded'          => false,
                    'multiple'          => false,
                ))...
                ...
        }
        ...
}

现在问题:

Symfony tries to get the choices for initialState EntityType field out of the Database: An exception occurred while executing 'SELECT t0_.id AS id_0, t0_.key AS key_1, t0_.order AS order_2, t0_.workflow_id AS workflow_id_3 FROM testpra_State t0_'.

But I want to get the choices from the states property of the Workflow object $workflowI pass to createForm in the controller.

类似于:

->add('initialState', EntityType::class, array(
    'class'             => State::class,
    'choice_label'      => 'key',
    'choice_value'      => 'key',
    'choices'           => $workflow->getStates(),

    // This combination of 'expanded' and 'multiple' implements a select box
    'expanded'          => false,
    'multiple'          => false,
))

我知道我可以像这样使用 $option 数组:

$secondFormPart = $this->createForm(PraWorkflowTransitionsType::class, $workflow, array(
    'states'    => $workflow->getStates()
));

但是有更简单的解决方案吗?

如果是,我如何才能访问我在子表单中传递给 createForm 的对象?

所以你必须传递一个数组给表单

public function createWorkflowAction(Request $request, Workflow $workflow) {
    $secondFormPart = $this->createForm(WorkflowType::class,null,array('workflow'=>$workflow));
    ... }

并将选项添加到解析器

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Workflow',
        'workflow' => ''
    ));
}

那么它将可以通过 $options['workflow'] 而不是 $workflow

访问

But is there a simpler solution?

如评论中的 kunicmarko20 所述,可通过 $options['data']:

访问该对象
public function buildForm(FormBuilderInterface $builder, array $options) {

    /** @var Workflow $workflow */
    $workflow = $options['data'];
    ...
}

他还指出我应该使用 ChoiceType 哪个更好,因为 EntityTypedesigned to load options from a Doctrine entity

And if yes how can I get access to the object I pass to createForm in a subform?

在我的例子中,我有一个 CollectionType 字段,它动态添加 entry_type TransitionType 的形式。在这种情况下,您可以通过 entry_options 属性传递数据:

->add('transitions', CollectionType::class, array(
    'entry_type'        => TransitionType::class,
    'entry_options'     => array(
        'states'    => $workflow->getStates(),
    ),
    ...
))

并在 TransitionType class:

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

    /** @var ArrayCollection $states */
    $states = $options['states'];
    ...
}