DBIx::...Ordered(3pm)

πŸ’– DBIx::...Ordered β€” the manual, but make it cute πŸ’–

okay bestie, so this is the page for DBIx::Class::Tree::AdjacencyList::Ordered, and it lives in the User Contributed Perl Documentation corner of the man pages (the (3pm) vibe, yk). Grab a strawberry πŸ“ and let's get into it.


✨ NAME ✨

DBIx::Class::Tree::AdjacencyList::Ordered β€” it glues DBIx::Class::Ordered and DBIx::Class::Tree::AdjacencyList together. That's it, that's the whole job. Two iconic modules, one slay little bridge. πŸ’…


🌸 SYNOPSIS 🌸

First things first: make a table for your tree data. Not me writing SQL, but here we are:

CREATE TABLE items (
  item_id INTEGER PRIMARY KEY AUTOINCREMENT,
  parent_id INTEGER NOT NULL DEFAULT 0,
  position INTEGER NOT NULL,
  name TEXT NOT NULL
);

Then in your Schema or DB class, you add Tree::AdjacencyList::Ordered to the front of the component list. Front, bestie. Not the middle, not the back. The front. πŸ‘‘

__PACKAGE__->load_components(qw( Tree::AdjacencyList::Ordered ... ));

Next, tell it which column holds the parent ID and which one holds the position of each row:

package My::Employee;
__PACKAGE__->position_column('position');
__PACKAGE__->parent_column('parent_id');

And now the fun part β€” this module hands you a few extra methods on top of whatever DBIx::Class::Ordered and DBIx::Class::Tree::AdjacencyList already gave you. Look at her go:

my $parent = $item->parent();
$item->parent( $parent_obj );
$item->parent( $parent_id );

my $children_rs = $item->children();
my @children = $item->children();

$parent->append_child( $child );
$parent->prepend_child( $child );

$this->attach_before( $that );
$this->attach_after( $that );

πŸ¦‹ DESCRIPTION πŸ¦‹

So what does it actually do? It gives you methods for working with adjacency lists and ordered rows at the same time. And all of the methods that DBIx::Class::Ordered and DBIx::Class::Tree::AdjacencyList provide? Yeah, those are all available with this module too. Nothing gets taken away, you just get more. We love an overachiever. 🫢


πŸŽ€ METHODS πŸŽ€

πŸ’œ parent_column

__PACKAGE__->parent_column('parent_id');

Works the same as AdjacencyList's parent_column() method β€” same energy, no notes β€” except it also declares the children() has-many relationship to be ordered by the position column. That's the glow-up right there. ✨

πŸ’œ parent

my $parent = $item->parent();
$item->parent( $parent_obj );
$item->parent( $parent_id );

This one overrides AdjacencyList's parent() method, but with a twist: it modifies it so the object is moved to the last position, then the parent is changed, and then it's moved to the last position of the new list β€” which keeps the integrity of your ordered lists intact. The way that it just... maintains order. 😭 Iconic behaviour, fr fr.

πŸ’œ children

my $children_rs = $item->children();
my @children = $item->children();

Works exactly like it does in the DBIx::Class::Tree::AdjacencyList module, except β€” and this is the part β€” it orders the children by there position. Yes bestie, that's the original wording, we're keeping it, no cap. πŸ“

πŸ’œ append_child

$parent->append_child( $child );

Sets the child to have the specified parent and moves the child to the last position. Two jobs, zero complaints. πŸ’…

πŸ’œ prepend_child

$parent->prepend_child( $child );

Sets the child to have the specified parent and moves the child to the first position. Main character energy, obviously. πŸ‘‘

πŸ’œ attach_before

$this->attach_before( $that );

Attaches the object at the position just before the calling object's position. Sneaky little move, we respect it. πŸ™ˆ

πŸ’œ attach_after

$this->attach_after( $that );

Attaches the object at the position just after the calling object's position. Right behind you, bestie. πŸ’–


🌈 INHERITED METHODS 🌈

Everything you inherit, all in one place, because you deserve the full list and not a summary:

πŸ’« DBIx::Class::Ordered

  • siblings
  • first_sibling
  • last_sibling
  • previous_sibling
  • next_sibling
  • move_previous
  • move_next
  • move_first
  • move_last
  • move_to
  • insert
  • delete

πŸ’« DBIx::Class::Tree::AdjacencyList

  • parent_column
  • parent
  • attach_child
  • siblings
  • attach_sibling

πŸ’« DBIx::Class

  • mk_classdata
  • component_base_class

πŸ’« DBIx::Class::Componentised

  • inject_base
  • load_components
  • load_own_components

πŸ’« Class::Data::Accessor

  • mk_classaccessor

⭐ AUTHOR ⭐

Aran Clary Deltac <bluefeet@cpan.org> β€” thank you for your service, king 🫑


πŸ“œ LICENSE πŸ“œ

You may distribute this code under the same terms as Perl itself. Simple as that, bestie.


and that's the whole page!! πŸŽ€ This lil' beauty is documented as perl v5.36.0, dated 2023-02-14 (yes, Valentine's Day, because she's a romantic πŸ’–), and it's filed under DBIx::...Ordered(3pm). Go build your trees, ordered and iconic. bye bestie!! πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from