按标题排列树Gedmo Tree Symfony [英] Sort tree by title Gedmo Tree Symfony

查看:176
本文介绍了按标题排列树Gedmo Tree Symfony的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按名称显示我的类别树。我有一个CategoryType来创建新的类别实体,并使用选择框为其分配一个父类。这是我的表单类型:

  / ** 
* @param FormBuilderInterface $ builder
* @param array $ options
* /
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder-> add('name',TextType :: class,array (required=> false));
$ builder-> add('parent',EntityType :: class,array(
'class'=>'CAPShopAdminBundle:Category',
'choice_label'=>'selectBoxName ',
'placeholder'=>' - Aucune - ',
'required'=> false,
'query_builder'=> function(NestedTreeRepository $ r){
return $ r-> createQueryBuilder('c')
- > orderBy('c.root,c.lvl,c.name','ASC');
}
));
$ builder-> add('save',SubmitType :: class);
}

结果如下:





我搜索此结果:





是否可以没有PHP处理,只需要很好的SQL查询?

解决方案

我想我已经找到了一个解决方案。我从一个唯一的根节点开始重建我的树。此结构更好,然后当我在树中插入一个节点时,我从Gedmo树Bundle执行reorder()函数。

  / ** 
* @param类别$ category
* /
public function saveCategory(Category $ category)
{

if(! $ category-> getId()){// insert
$ this-> objectManager-> persist($ category);
$ this-> objectManager-> flush();

$ root = $ this-> categoryRepository-> findOneBySlug('pieces-cars'); //根节点
$ this-> categoryRepository-> reorder($ root,'name','ASC'); // Reoder by name
}

$ category-> setSlug($ category-> getId().'-'。$ this->杂项 - > slugify($分类 - >的getName(), ' - '));

$ this-> objectManager-> flush();

}

我的树很小,最多可能有30个节点。所以我可以在每次插入节点时重新编译树。但是要小心,因为reorder()是一个很重的功能,可以用大树来花费一些时间。



我得到的树没有根:

  / ** 
* @return bool
* /
public function getAllCategories(){

$ root = $ this-> categoryRepository-> findOneBySlug('pieces-cars');

$ htmlTree = $ this-> categoryRepository-> childrenHierarchy(
$ root,/ *从根节点开始* /
false,/ *孩子* /
数组(
'decorate'=> true,
'representationField'=>'name',
'html'=> true,

);

return $ htmlTree;
}

结果:




I want to display my Categories tree sort by names. I have a CategoryType to create new Category entity and assignate it a parent Category with a select box. Here's my form type :

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name', TextType::class, array("required" => false));
    $builder->add('parent', EntityType::class, array(
        'class' => 'CAPShopAdminBundle:Category',
        'choice_label' => 'selectBoxName',
        'placeholder' => '-- Aucune --',
        'required' => false,
        'query_builder' => function(NestedTreeRepository $r) {
            return $r->createQueryBuilder('c')
                ->orderBy('c.root, c.lvl, c.name', 'ASC');
        }
    ));
    $builder->add('save', SubmitType::class);
}

Here's the result :

I search this result :

Is it possible without PHP treatment, just with the good SQL query?

解决方案

I think I've find a solution. I've rebuild my tree with a start from one and only root node. This structure is better, and then, when I insert a node in the tree, I execute the reorder() function from Gedmo tree Bundle.

    /**
 * @param Category $category
 */
public function saveCategory(Category $category)
{

    if(!$category->getId()){ //insert
        $this->objectManager->persist($category);
        $this->objectManager->flush();

        $root = $this->categoryRepository->findOneBySlug('pieces-automobile'); //The root node
        $this->categoryRepository->reorder($root, 'name', 'ASC'); //Reoder by name
    }

    $category->setSlug($category->getId().'-'.$this->miscellaneous->slugify($category->getName(),'-'));

    $this->objectManager->flush();

}

My tree will be pretty small, maybe 30 nodes maximum. So I can reoder the tree each time I insert a node. But be careful, because reorder() is a heavy function who can takes some times with big tree.

I get my tree without the root with :

    /**
 * @return bool
 */
public function getAllCategories(){

    $root = $this->categoryRepository->findOneBySlug('pieces-automobile');

    $htmlTree = $this->categoryRepository->childrenHierarchy(
        $root, /* starting from root nodes */
        false, /* true to take only direct children */
        array(
            'decorate' => true,
            'representationField' => 'name',
            'html' => true,
        )
    );

    return $htmlTree;
}

And the result :

这篇关于按标题排列树Gedmo Tree Symfony的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆