在树中查找ID - 查询逻辑 - PHP

Dar*_*ney 7 php

我有以下表格文件夹:

  id    name    childOf
------------------------
  1      A        0
  2      B        1
  3      C        0
  4      D        3
  5      E        2
  6      F        5
Run Code Online (Sandbox Code Playgroud)

这形成了一棵树:

A
-B
--E
---F
C
-D
Run Code Online (Sandbox Code Playgroud)

我允许拖放文件夹,但需要防止文件夹被拖入自己的子文件夹.

例如D到B没问题,D到E没问题,B到F不能正常拖动到自己的树中,但是F到B可以正常拖动树.

问题:如果用户选择B并尝试将其拖动到F,我该如何防止?

我正在寻找逻辑,怎么会说,然后编码,B到F不行,但F到B是.

nie*_*fir 0

只需检查父级,直到找到根文件夹?在伪代码中

// targetFolder is the target folder where the folder is going to be moved
// while sourceFolder is the folder that's moved.
parents = targetFolder.getParents();
if (parents.contains(sourceFolder)) {
    // don't allow the operation
}
Run Code Online (Sandbox Code Playgroud)