如何使用" sonata_type_model"使用选项组呈现多选下拉列表或者symfony" entity"?

时间:2015-07-04 04:19:18

标签: php forms symfony sonata-admin

我尝试使用sonata_type_modelentity类型呈现类似于the SO question的多项选择下拉菜单。我的实体PropertymanyToManyFeature oneToMany FeatureType。多选下拉列表正在Property property表单中实现。

我找到了the group_by option。但是我使用entity收到了警告,并使用sonata_type_model得到了错误的渲染。

尝试1

->add('features', 'entity', array(
    'class' => 'MyBundle:Feature',
    'multiple' => true,
    'group_by' => 'featureType'
))
  

警告:isset中的非法偏移类型或者在第334行的vendor / symfony / symfony / src / Symfony / Component / Form / ChoiceList / Factory / DefaultChoiceListFactory.php中为空

尝试2

->add('features', 'sonata_type_model', array(
    'multiple' => true,
    'btn_add' => null,
    'group_by' => 'featureType'
))

它只渲染下拉列表中的值,例如

enter image description here

尝试3

我尝试了choices,但收到了错误

->add('features', 'sonata_type_model', array(
    'multiple' => true,
    'required' => false,
    'btn_add' => null,
    'choices' => $this->getFeatureOptionsWithGroup()
))
  

警告:get_class()期望参数1是对象,在第58行的vendor / doctrine / common / lib / Doctrine / Common / Util / ClassUtils.php中给出的数组

尝试4

我尝试了choice_list,但收到了错误

->add('features', 'sonata_type_model', array(
    'multiple' => true,
    'required' => false,
    'btn_add' => null,
    'choice_list' => $this->getFeatureOptionsWithGroup()
))
  

选项" choice_list"值数组应该是" null"或" Symfony \ Component \ Form \ ChoiceList \ ChoiceListInterface",但类型为" array"在vendor / symfony / symfony / src / Symfony / Component / OptionsResolver / OptionsResolver.php第888行

方法getFeatureOptionsWithGroup()像这样返回数组

array(
    'Group 1' => array(
        1 => 'Feature 1'
        2 => 'Feature 2'
    ),
    'Group 2' => array(
        3 => 'Feature 3'
        4 => 'Feature 4'
    )
)

尝试5

我使用ModelChoiceList更新了该方法,但在使用choices时遇到了同样的错误。

  

警告:get_class()期望参数1是对象,在第58行的vendor / doctrine / common / lib / Doctrine / Common / Util / ClassUtils.php中给出的数组

private function getFeatureOptionsWithGroup()
{
    return new ModelChoiceList(
        $this->modelManager,
        'MyBundle:Feature',
        null,
        null,
        $this->getRepository('MyBundle:Feature')->getFeatureOptionsWithGroup()
    );
}

我希望我有机会在没有模板覆盖的情况下渲染它。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

使用类型Dim cnn As New OleDb.OleDbConnection cnn = New OleDb.OleDbConnection cnn.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Users\Ashe\Documents\Visual Studio 2010\Projects\WindowsApplication1\Guidance System.accdb") Dim mydate As DateTime mydate = DateTimePicker1.Value mydate.ToString("yyyy-MM-dd") Dim AddCol = "ALTER TABLE Attendance " & _ "ADD '" & mydate & "' varchar(100);" Using cmd = New OleDbCommand(AddCol, cnn) cnn.Open() cmd.ExecuteNonQuery() End Using

entity

确保您的函数->add('features', 'entity', [ 'class' => 'MyBundle:Feature', 'property' => 'name', 'choices' => $this->getFeatureOptionsWithGroup(), 'multiple' => true, 'required' => false, 'btn_add' => null, ]) 像这样返回

getFeatureOptionsWithGroup()

答案 1 :(得分:1)

执行此操作的最佳方法是使用group_by选项。

因此,根据您的情况,您需要在$property实体中传递FeatureType字段的名称,然后在您的Property实体上传递,您需要实现__toString()函数,该函数应该是属性为字符串。

解释

FeatureType实体表上,属性关联由property_id映射为外键,Doctrine为您查询,因此您只需指定要对其进行分组的方式。