{"id":1365,"date":"2015-03-14T22:55:48","date_gmt":"2015-03-14T22:55:48","guid":{"rendered":"http:\/\/fbcs.co.uk\/?p=1365"},"modified":"2015-03-14T23:26:13","modified_gmt":"2015-03-14T23:26:13","slug":"using-dbixclasstreenestedset","status":"publish","type":"post","link":"https:\/\/wp.fbcs.co.uk\/using-dbixclasstreenestedset\/","title":{"rendered":"Using DBIx::Class::Tree::NestedSet"},"content":{"rendered":"

\n\tI’ve been struggling to get DBIx::Class::Tree::NestedSet<\/a> working today because I found the documentation a bit sparse.\n<\/p>\n

\n\tThe problem is basically that the instructions assume that I’m very familiar with DBIx::Class, and that therefore I don’t need much detail.\n<\/p>\n

\n\tAfter much trial and error I got it to work.\n<\/p>\n

\n\tSo these notes are aimed at someone who has got the basics of DBIx::Class<\/code><\/a> running, with all the clever schema classes created (either manually or via something like dbicdump<\/code><\/a>).\n<\/p>\n

\n\tFor this example, the tree is in a table called ‘treetable’, whose schema is defined in App::DB::Schema<\/code>.  The table needs to have the following columns, in addition to the ones that you need for your application:\n<\/p>\n

\r\n\r\n  id       INTEGER PRIMARY KEY AUTOINCREMENT,\r\n  root_id  integer,\r\n  lft      integer NOT NULL,\r\n  rgt      integer NOT NULL,\r\n  level    integer NOT NULL,<\/code>\r\n<\/pre>\n

\n\tThis line:\n<\/p>\n

\r\n__PACKAGE__->load_components(qw( Tree::NestedSet ...<\/span> ));<\/pre>\n

\n\tneeds to go near the bottom of App::DB::Schema.pm, after the ‘DO NOT MODIFY THIS OR ANYTHING ABOVE!<\/code>‘ line.  Note that the ellipsis (...<\/span><\/code>) needs to be either deleted or replaced with any components that you’re already using.\n<\/p>\n

\n\tThese lines:\n<\/p>\n

\r\n__PACKAGE__->tree_columns({\r\n    root_column  => 'root_id',\r\n    left_column  => 'lft',\r\n    right_column => 'rgt',\r\n    level_column => 'level',\r\n});<\/code><\/pre>\n

\n\tgo in App::DB::Schema::Result::Treetable.pm<\/code>, after the ‘DO NOT MODIFY THIS OR ANYTHING ABOVE!<\/code>‘ line, and with the column names set to match the ones in your table.\n<\/p>\n

\n\tThat same file, App::DB::Schema::Result::Treetable.pm<\/code>, is the one that needs\n<\/p>\n

\r\nuse parent DBIx::Class::Tree::NestedSet;<\/code><\/pre>\n

\n\tnear the top.\n<\/p>\n

\n\tOnce that all compiles cleanly, you can start accessing the tree table.\n<\/p>\n

\n\tStart by creating the result set in the usual DBIC way:\n<\/p>\n

\r\nmy $rs = $schema->resultset('Treetable');<\/pre>\n

\n\twhere $schema<\/code> is your database handle.\n<\/p>\n

\n\tThen you can start using the tree relationships and methods however you like.  Note that the documentation uses $node<\/code> to refer to each node in the tree: each such $node<\/code> is a DBIC object that can be used to access the fields from the corresponding database row, navigate to other nodes (children, siblings, etc.), or create new children or siblings.\n<\/p>\n

\n\tFor example,\n<\/p>\n

\r\nmy $first = $rs->first;  # get the first (or only) root node\r\nmy $desc_rs = $first->descendants;\r\nwhile (my $desc = $desc_rs->next) { \r\n    print '**'x$desc->level, \" id=\", $desc->id, \r\n        \", field1=\", $desc->name, \"\\n\";\r\n}<\/code><\/pre>\n

\n\tI hope that makes sense.<\/p>\n","protected":false},"excerpt":{"rendered":"

I’ve been struggling to get DBIx::Class::Tree::NestedSet working today because I found the documentation a bit sparse. The problem is basically that the instructions assume that I’m very familiar with DBIx::Class, and that therefore I don’t need much detail. After much trial and error I got it to work. So these notes are aimed at someone […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[59,46],"tags":[],"_links":{"self":[{"href":"https:\/\/wp.fbcs.co.uk\/wp-json\/wp\/v2\/posts\/1365"}],"collection":[{"href":"https:\/\/wp.fbcs.co.uk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.fbcs.co.uk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.fbcs.co.uk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.fbcs.co.uk\/wp-json\/wp\/v2\/comments?post=1365"}],"version-history":[{"count":14,"href":"https:\/\/wp.fbcs.co.uk\/wp-json\/wp\/v2\/posts\/1365\/revisions"}],"predecessor-version":[{"id":1379,"href":"https:\/\/wp.fbcs.co.uk\/wp-json\/wp\/v2\/posts\/1365\/revisions\/1379"}],"wp:attachment":[{"href":"https:\/\/wp.fbcs.co.uk\/wp-json\/wp\/v2\/media?parent=1365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.fbcs.co.uk\/wp-json\/wp\/v2\/categories?post=1365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.fbcs.co.uk\/wp-json\/wp\/v2\/tags?post=1365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}