良い一日の仲間!現在、Modified Pre-order Tree Traversal(MPTT)と呼ばれるこのデータベースモデルの設計を研究しています。パフォーマンスが低いためにCommonTableExpressions(CTE)を使用することの欠点を確認した後、MPTTを使用することをお勧めします。ただし、MPTTの利点を利用する前に、「右」と「左」のノード値を追加してデータベーステーブルを再設計する必要があります。そのためには、テーブル内の各データの値を自動化および更新するプログラムを作成する必要があります。私の問題は、ノード値を自動化するプログラムを作成できないことです。PHP言語をC#コードに変換しようとしていますが、変換できませんでした。プログラミングの私の弱点の1つは、「再帰的」メソッドを作成することです。
このリンクを参照として使用しています。 階層型データベースモデル
そして、これが私がC#に変換しようとしているコードです
<?php
function rebuild_tree($parent, $left) {
// the right value of this node is the left value + 1
$right = $left+1;
// get all children of this node
$result = mysql_query('SELECT title FROM tree '.
'WHERE parent="'.$parent.'";');
while ($row = mysql_fetch_array($result)) {
// recursive execution of this function for each
// child of this node
// $right is the current right value, which is
// incremented by the rebuild_tree function
$right = rebuild_tree($row['title'], $right);
}
// we've got the left value, and now that we've processed
// the children of this node we also know the right value
mysql_query('UPDATE tree SET lft='.$left.', rgt='.
$right.' WHERE title="'.$parent.'";');
// return the right value of this node + 1
return $right+1;
}
?>