Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony Sonata Admin使用自定义表单类型配置表单字段_Symfony_Sonata Admin - Fatal编程技术网

Symfony Sonata Admin使用自定义表单类型配置表单字段

Symfony Sonata Admin使用自定义表单类型配置表单字段,symfony,sonata-admin,Symfony,Sonata Admin,我有两个实体: 1。问题 /** * @ORM\Table() * @ORM\Entity */ class Question { /** * @ORM\Column(type="smallint", nullable=false) */ private $type; /** * @ORM\Column(type="string", length=255) */ private $test_field; /

我有两个实体:

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;
}
/**
 * @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;
}
2。选项

/**
 * @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;
}
/**
 * @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;
}
根据
问题
类型,需要使用不同的formType,其中只包含部分
选项
字段

例如:

带有
type=X的
Question
应具有带有字段的表单
$field\u for\u Question\u type\u X

带有
type=Y的
Question
应具有带有字段的表单
$field\u for\u Question\u type\u Y

等等

我还创建了这些不同的表单类型,所以问题是如何让Sonata Admin
$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;
        }
    }
}

应该添加什么来提供这种功能?

答案很简单:

->add('options', 'sonata_type_collection',
    array(),
    array(
        'edit' => 'inline',
        'inline' => 'table',
        'admin_code' => $type_based_admin_code # each has custom form configuration
    )
)

答案很简单:

->add('options', 'sonata_type_collection',
    array(),
    array(
        'edit' => 'inline',
        'inline' => 'table',
        'admin_code' => $type_based_admin_code # each has custom form configuration
    )
)