[英]Sort Gedmo Tree by arbitrary field using symfony4, doctrine2 and stofDoctrineExtensionsBundle
For my personal project i am using:对于我的个人项目,我正在使用:
Doctrine v2.6.3学说 v2.6.3
StofDoctrineExtensionsBundle in order to manage Tree structure. StofDoctrineExtensionsBundle以管理树结构。
I did setup the tree structure (that represents directories and files) called FileTree
.我确实设置了名为
FileTree
的树结构(代表目录和文件)。 I added several custom fields to the tree: item_name
, item_extension
, item_sort_value
and is_file
.我向树中添加了几个自定义字段:
item_name
、 item_extension
、 item_sort_value
和is_file
。
I can not get folders and files sorted by field item_sort_value
in the fileTree!我无法在
item_sort_value
中获取按字段item_sort_value
排序的文件夹和文件!
At the same time i can sort items (as is needed) when only retrieving files inside custom filder.同时,当仅检索自定义过滤器中的文件时,我可以对项目进行排序(根据需要)。
Retrieving (full size) folder file tree
检索(全尺寸)文件夹文件树
public function getFileTreeNodeArray($file_tree_root_id): array
{
$em = $this->getEntityManager();
$query = $em
->createQueryBuilder()
->select('ft')
->from(FileTree::class, 'ft')
->where('ft.root = :root_id')
->setParameter('root_id', $file_tree_root_id)
->orderBy('ft.root, ft.lft', 'ASC')
//->addOrderBy('ft.itemSortValue', 'ASC')
->getQuery();
$query->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true);
return $query->getArrayResult();
}
Retrieving files in the folder
检索文件夹中的文件
public function getFileTreeNodeContentFilesByUploaderArray($file_tree_node_id, $user_id): array
{
$em = $this->getEntityManager();
$is_file = true;
$query = $em
->createQueryBuilder()
->select('ft')
->from(FileTree::class, 'ft')
->where('ft.parent = :file_tree_node_id')
->andWhere('ft.isFile = :is_file')
->andWhere('ft.uploader = :user_id')
->setParameter('file_tree_node_id', $file_tree_node_id)
->setParameter('is_file', $is_file)
->setParameter('user_id', $user_id)
->orderBy('ft.itemSortValue', 'ASC')
->getQuery();
$query->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true);
return $query->getArrayResult();
}
I tried to use reorder
and reorderAll
, but "to no avail".我尝试使用
reorder
和reorderAll
,但“无济于事”。
An example
一个例子
public function reorderFileTreeBranch($parent_id): void
{
$repo_file_tree = $this->entityManager->getRepository(FileTree::class);
$branch_node = $repo_file_tree->findOneBy(['id' => $parent_id]);
$repo_file_tree->reorder($branch_node, 'item_sort_value', 'ASC');
$this->entityManager->flush();
}
i can see many additional SQL queries (whenever i use reorder
or reorderAll
) in the Symfony profiler
, so something is definitely going on, but after that - items are sorted the same in the tree...我可以在
Symfony profiler
看到许多额外的 SQL 查询(每当我使用reorder
或reorderAll
),所以肯定会发生一些事情,但在那之后 - 项目在树中的排序相同......
Am i doing something wrong?难道我做错了什么?
Ideas and answers are welcome!欢迎提出想法和答案!
Well, after many tries and fails i managed to got it working as expected.好吧,经过多次尝试和失败,我设法让它按预期工作。 Turned out that
reorder
in NestedTreeRepositoryTrait
did not return true on success... I fixed it - and so - now ordering woks as intended.结果发现
NestedTreeRepositoryTrait
中的reorder
在NestedTreeRepositoryTrait
时没有返回 true ......我修复了它 - 所以 - 现在按预期订购炒锅。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.