DB_NestedSet (Previous) (Next) DB_NestedSet::addListener

View this page in Last updated: Sun, 05 Oct 2008
English | French | German | Japanese | Plain HTML

導入

導入 --  DB_NestedSet について

概要

このパッケージを使用すると、 リレーショナルデータベース内に 任意の深さのツリーを作成することができます。 このパッケージには、次のような機能があります。

  • ノードの作成/更新/削除

  • ノードやツリー、サブツリーに対する問い合わせ

  • ノードやツリー、サブツリーのコピー (クローン)

  • ノードやツリー、サブツリーの移動

  • その他

例 39-1 ルートおよびサブノードの作成

この例では、ひとつのルートノードとふたつのサブノードを作成し、 それをデータベースに格納して表示します。


<?php
require_once 'DB/NestedSet.php';
require_once 'DB/NestedSet/Output.php';
require_once 'HTML/Menu.php';
$DatabasePointer mysql_connect("localhost""user""pwd");
mysql_select_db("database"$DatabasePointer);
$dsn 'mysql://user:pwd@localhost/database';
// テーブルに必要なカラム
$params = array(
    'id'        => 'id',
    'parent_id' => 'rootid',
    'left_id'   => 'l',
    'right_id'  => 'r',
    'order_num' => 'norder',
    'level'     => 'level',
    'name'      => 'name',
);
$nestedSet =& DB_NestedSet::factory('DB'$dsn$params);
$nestedSet->setAttr(array(
        'node_table' => 'nested_set',
        'lock_table' => 'nested_set_locks',
        'secondarySort' => 'name',
    )
);
$parent $nestedSet->createRootNode(array('name' =>'root 1'), falsetrue);
$nestedSet->createSubNode($parent, array('name' => 'node 1.1'));
$nestedSet->createSubNode($parent, array('name' =>'node 1.2'));
$data $nestedSet->getAllNodes(true);

foreach ($data as $id => $node) {
     $data[$id]['url'] = 'index.php?nodeID=' $node['id'];
}

$params = array(
    'structure' => $data,
    'titleField' => 'name',
    'urlField' => 'url');
$output =& DB_NestedSet_Output::factory($params'Menu');
$structure $output->returnStructure();
$menu = & new HTML_Menu($structure'sitemap');
$menu->forceCurrentUrl($currentUrl);
$menu->show();
?>

DB_NestedSet (Previous) (Next) DB_NestedSet::addListener

Download Documentation Last updated: Sun, 05 Oct 2008
Do you think that something on this page is wrong? Please file a bug report or add a note.
User Notes:
Note by: wabnitz@platis.de
You need to create two tables. The names of the tables are passed in through the $nestedSet->setAttr() method.

One table for the nodes has columns as described in the params array.

The second table for locking needs following columns:
lockId - VARCHAR(32)
lockTable - VARCHAR(32)
lockStamp - TIMESTAMP