1

私は2つのエンティティを持っています:

1. 質問

/**
 * @ORM\Table()
 * @ORM\Entity
 */
class Question
{
    /**
     * @ORM\Column(type="smallint", nullable=false)
     */
    private $type;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $test_field;

    /**
     * @ORM\OneToMany(targetEntity="Option", mappedBy="question")
     */
    private $options;
}

2. オプション

/**
 * @ORM\Table()
 * @ORM\Entity
 */
class Option
{
    /**
     * @ORM\ManyToOne(targetEntity="Question", inversedBy="options")
     * @ORM\JoinColumn(name="question_id", referencedColumnName="id")
     */
    private $question;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $field_for_question_type_x;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $field_for_question_type_y;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $field_for_question_type_z;
}

タイプに基づいて、一部のフィールドQuestionのみを含む異なる formType を使用する必要があります。Option

例えば:

Questionwithtype = Xフィールドを持つフォームを持つ必要があります$field_for_question_type_x

Questionwithtype = Yフィールドを持つフォームを持つ必要があります$field_for_question_type_y

私はそれらの異なるフォームタイプも作成しているので、質問は、Sonata管理者にそのようなフォームのコレクションを追加するように指示するにはどうすればよいですか$formMapper(質問エンティティタイプに基づいて)?

現在、次のようになっています。

protected function configureFormFields(FormMapper $formMapper)
{
    $Question = $this->getSubject();

    $formMapper->add('test_field');

    if ($Question->getId()) {
        $formMapper->with('Options');

        switch ($Question->getType()) {
            case 'X':
                // Here I would need to add collection of \My\Bundle\Form\OptionXType()
                $formMapper->add('options', ????????);
                break;
            case 'Y':
                // Here I would need to add collection of \My\Bundle\Form\OptionYType()
                $formMapper->add('options', ????????);
                break;
        }
    }
}

そのような機能を提供するには何を追加する必要がありますか?

4

1 に答える 1