【问题标题】:Sonata Admin configureFormFields using custom form typesSonata Admin configureFormFields 使用自定义表单类型
【发布时间】:2013-11-15 10:57:00
【问题描述】:

我有 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 字段。

例如:

Questiontype = X 应该具有带有字段 $field_for_question_type_x 的表单

Questiontype = Y 应该具有带有字段 $field_for_question_type_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;
        }
    }
}

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

【问题讨论】:

    标签: symfony sonata-admin


    【解决方案1】:

    答案很简单:

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

    【讨论】:

      猜你喜欢
      • 2012-07-15
      • 2013-09-06
      • 1970-01-01
      • 2013-12-19
      • 2018-07-22
      • 2018-12-25
      • 2014-01-29
      • 2017-09-02
      • 2019-03-17
      相关资源
      最近更新 更多