【问题标题】:Formatting choice list options in entity form type格式化实体表单类型中的选项列表选项
【发布时间】:2012-07-02 12:00:20
【问题描述】:

我正在使用基于选择的实体表单类型,以便在 Sonata 管理表单中提供选择列表。我想要的是使用子属性来显示选择列表中的元素集合。

有谁知道如何提供要在选择列表中显示的标签或如何为选择列表数组提供格式?

    $formMapper->add( 'frArea', 'entity', array(
        'class'             => 'myVendor\myBundleBundle\View\myEntity',
        //When using a child property here, choice list gets corrupted/bad formatted
        //'property'            => 'child.property',
        'property'          => 'property',
        'em'                => 'formacions',
        'query_builder'     =>  function( EntityRepository $er )
                                {
                                    return $er->createQueryBuilder( 'qb' )
                                              ->add( 'select', 'm' )
                                              ->add( 'from', 'myVendor\myBundleBundle\View\myEntity m' );
                                              //->leftJoin('a.idiomes', 'ai');
                                },
        'required'          => false, 
        'label'             => ucfirst( $this->trans( 'my_label', array(), $this->translationDomain, $this->langCode ) )
    ) );  

当以下列方式使用属性选项'property' => 'child.property'我得到以下错误:

Neither property "nom" nor method "getProperty()" nor method "isProperty()" exists in class "Doctrine\ORM\PersistentCollection"

【问题讨论】:

    标签: symfony symfony-forms symfony-sonata


    【解决方案1】:

    这就是参数property的作用!不需要告诉 Symfony 哪个字段是选项的值,它需要 ID 列,所以property 用于显示;)

    【讨论】:

    • 谢谢,但正如您在帖子中看到的那样,当属性 alue 是子实体属性时,我遇到了问题。我已经编辑了帖子以显示我这样做时遇到的错误。
    • 您是否尝试将公共访问器从 MyEntity m 写入相关子实体?
    • 不,将尝试在此处发布。谢谢!
    • 不是我想要的解决方案,但至少得到了我想要的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 2023-03-16
    • 1970-01-01
    • 2013-06-07
    • 2013-10-07
    • 2015-04-03
    • 1970-01-01
    相关资源
    最近更新 更多