创建树嵌套选择选项

时间:2014-08-02 01:53:21

标签: symfony doctrine-orm tree

我有一个实体类别,我使用Tree Gedmo扩展来管理它。一切正常(添加,编辑和删除),但我想将选择选项显示为树格式,如下所示:

Category 1
- SubCategory 1
- SubCategory 2
Category 2
- Subcategory 3
- SubCategory 4
Category 3
Category 4

我使用了方法 getChildrenQueryBuilder ,但总是给我:

Category 1
Category 2
Category 3
Category 4

这是FormType

<?php

namespace Project\StoreBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;

class CategoryType extends AbstractType
{
    /**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'text', array(
         'required' => true,
         'label' => "Name",
         'attr' => array('class' =>'col-sm-8'),
         ))
        ->add('enabled', 'checkbox', array(
         'required' => false,
         'label' => "Enable",
         'attr' => array('class' =>'ace ace-switch ace-switch-7', 'checked' => 'checked'),
         ))
        ->add('description', 'textarea', array(
         'required' => false,
         'label' => "Description",
         'attr' => array('class' =>'col-sm-8'),
         ))
        ->add('metaDescription', 'text', array(
         'required' => false,
         'label' => "Meta Description",
         'attr' => array('class' =>'col-sm-8'),
         ))
        ->add('metaKeywords', 'text', array(
         'required' => false,
         'label' => "Meta key-words",
         'attr' => array('class' =>'form-control form-field-tags', 'placeholder' =>'Enter tags ...'),
         ))
        ->add('parent', 'entity', array(
        'required' => false,
        'label' => 'Category parent',
        'class' => 'ProjectStoreBundle:Category',
        'attr' => array('class' => 'col-sm-8'),
        'empty_value' => 'Select one category',
        'property' => 'name',
        'multiple' => false,
        'expanded' => false ,
        'query_builder' => function (\Project\StoreBundle\Entity\CategoryRepository $r)
            {
                return $r->getChildrenQueryBuilder();
            }
        ))
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Project\StoreBundle\Entity\Category'
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'project_storebundle_category';
}
}

2 个答案:

答案 0 :(得分:4)

你可以这样做:

1)将非映射属性添加到Category实体 private $indentedName;

2)向您的类别实体添加方法

public function getIndentedName() {
    return str_repeat($this->parent." > ", $this->lvl) . $this->name;
}

3)更改CategorType 'property' => 'indentedName'

上的属性

修改: 我为'query_builder' => $indentedCatTree

添加了我的Closure
$indentedCatTree = function(CategoriesRepository $er) {
    return $er->getChildrenQueryBuilder(null, null, 'root', 'asc', false);
};

PS :您可以修改方法getIndentedName()以显示缩进方式

答案 1 :(得分:1)

您可以尝试这样的事情(我没有测试过):

->add('parent', 'entity', array(
    'required' => false,
    'label' => 'Category parent',
    'class' => 'ProjectStoreBundle:Category',
    'attr' => array('class' => 'col-sm-8'),
    'empty_value' => 'Select one category',
    'property' => 'name',
    'multiple' => false,
    'expanded' => false ,
    'group_by' => 'parent',
    'query_builder' => function (EntityRepository $r)
        {
            $queryBuilder = $r->createQueryBuilder('c');
            $query = $queryBuilder
                ->where($queryBuilder->expr()->isNotNull('c.parent'))
            ;

            return $query;
        }
    ))