按标题排序树 Gedmo Tree Symfony
Sort tree by title Gedmo Tree Symfony
我想显示按名称排序的类别树。我有一个 CategoryType 来创建新的 Category 实体并为它分配一个带有 select 框的 parent Category。这是我的表单类型:
/**
* @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 tree Bundle 执行 reorder() 函数。
/**
* @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();
}
我的树会很小,最多可能有 30 个节点。所以我可以在每次插入节点时重新排序树。但是要小心,因为 reorder() 是一个很重的函数,对于大树来说可能需要一些时间。
我的树没有根:
/**
* @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;
}
结果:
我想显示按名称排序的类别树。我有一个 CategoryType 来创建新的 Category 实体并为它分配一个带有 select 框的 parent Category。这是我的表单类型:
/**
* @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 tree Bundle 执行 reorder() 函数。
/**
* @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();
}
我的树会很小,最多可能有 30 个节点。所以我可以在每次插入节点时重新排序树。但是要小心,因为 reorder() 是一个很重的函数,对于大树来说可能需要一些时间。
我的树没有根:
/**
* @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;
}
结果: