【问题标题】:Custom choices list of sonata_type_model field with Sonata AdminSonata Admin 的 sonata_type_model 字段的自定义选项列表
【发布时间】:2013-08-31 04:51:59
【问题描述】:

我正在使用 Sonata Admin,并且我有一个类别字段,我需要像选择中的树一样按顺序显示它们:

<select>
    <option>Category father-1</option>
    <option>--Category child-1-1</option>
    <option>--Category child-1-2</option>
    <option>--Category child-1-3</option>
    <option>----Category child-1-3-1</option>
    <option>----Category child-1-3-2</option>
    <option>--Category child-1-4</option>
    <option>--...</option>
    <option>Category father-2</option>
</select>

有可能吗?我已经尝试过在“choice_list”中包含一个在 getTreeCatsArray 方法中生成的数组:

protected function configureFormFields(FormMapper $formMapper)
{
    $tree_cat_array = $this->em->getRepository('MyBundle:Category')->getTreeCatsArray();

    $formMapper
        ->add('category', 'sonata_type_model', array(
                'empty_value' => '', 
                'choice_list' => $tree_cat_array)); 
}

这显示了错误:

The option "choice_list" with value "Array" is expected to be of type "null", "Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface"

我不确定是否必须使用字段类型“sonata_type_model”或“选择”

【问题讨论】:

    标签: symfony sonata-admin


    【解决方案1】:

    好的,我已经得到了按树排序的类别列表,以将其包含在相关实体中,如下所示:

    protected function configureFormFields(FormMapper $formMapper)
    {
        $em = $this->modelManager->getEntityManager('MyBundle\Entity\Category');
    
        $query = $em->createQueryBuilder('c')
                    ->select('c')
                    ->from('MyBundle:Category', 'c')
                    ->where('c.parent IS NOT NULL')
                    ->orderBy('c.root, c.lft', 'ASC');
    
        $formMapper
            ...
            ->add('categoria', 'sonata_type_model', array(
                'required' => true, 
                'query' => $query
            ))
            ...
        ; 
    }
    

    希望对大家有帮助

    【讨论】:

    • 我必须使用CompanyMyBundle:Category
    • 嗨,你试过这个要求 false 吗?我有并且一直得到无法设置 Null 值,除了 Categoria 并得到了 null,谢谢
    • 找到了我的解决方案。如果您希望它是“必需的”=> false。 setCategory(Category $category = null) 所以当表单提交时它不会将值设置为 null 并引发错误。
    【解决方案2】:

    试试:

    ->add('category', 'entity', array(
            'class'    => 'Acme\Entity\Category',
    )
    

    这仅在您拥有实体 Category 时才有效。

    请参阅 this article,了解为 SonataAdminBundleCategory 实体创建树编辑器。 Here 是同一篇俄语文章,但在第一个变体中包含缺失代码。

    【讨论】:

      【解决方案3】:

      阅读上述答案后,我必须执行以下操作才能获得 OP 所追求的功能:

      protected function configureFormFields(FormMapper $formMapper)
      {
      
      $em = $this->modelManager->getEntityManager('YourBundleFile\YourBundleFileBundle\Entity\YourEntity');
      
      $qb = $em->createQueryBuilder();
      
      $qb = $qb->add('select', 'u')
              ->add('from', 'YourBundleFile\YourBundleFileBundle\Entity\YourEntity u');
      
      $query = $qb->getQuery();
      $arrayType = $query->getArrayResult();
      
      $formMapper
      ->add('yourProperty', 'choice', array('choices'=>$arrayType))
      -end();
      }
      

      【讨论】:

        猜你喜欢
        • 2018-07-11
        • 1970-01-01
        • 1970-01-01
        • 2014-09-27
        • 2013-09-06
        • 1970-01-01
        • 2013-12-19
        • 2018-12-25
        • 1970-01-01
        相关资源
        最近更新 更多