DBIx::...inTable(3pm)

πŸ“¦
version 2.037000 Β· 5 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’– DBIx::Class::Helper::Row::JoinTable πŸ’–

hey bestie 🫢 welcome to DBIx::...inTable(3pm), straight outta the User Contributed Perl Documentation. it's giving "join tables but make it effortless" and honestly? obsessed.


✨ NAME ✨

DBIx::Class::Helper::Row::JoinTable β€” easily set up join tables with DBIx::Class. that's the whole vibe. πŸ’…


🌸 SYNOPSIS 🌸

okay so picture this: you've got a join table and you want it set up with zero suffering. look:

package MyApp::Schema::Result::Foo_Bar;

__PACKAGE__->load_components(qw{Helper::Row::JoinTable Core});

__PACKAGE__->join_table({
   left_class   => 'Foo',
   left_method  => 'foo',
   right_class  => 'Bar',
   right_method => 'bar',
});

# the above is the same as:

__PACKAGE__->table('Foo_Bar');
__PACKAGE__->add_columns(
   foo_id => {
      data_type         => 'integer',
      is_nullable       => 0,
      is_numeric        => 1,
   },
   bar_id => {
      data_type         => 'integer',
      is_nullable       => 0,
      is_numeric        => 1,
   },
);

$self->set_primary_key(qw{foo_id bar_id});

__PACKAGE__->belongs_to( foo => 'MyApp::Schema::Result::Foo' 'foo_id');
__PACKAGE__->belongs_to( bar => 'MyApp::Schema::Result::Bar' 'bar_id');

like… that's it. that's the whole thing. the second block is literally what the first block does behind the scenes β€” same energy, just longer πŸ¦‹

and if you're running DBIx::Class::Candy, it gets even cuter:

package MyApp::Schema::Result::Foo_Bar;

use DBIx::Class::Candy -components => ['Helper::Row::JoinTable'];

join_table {
   left_class   => 'Foo',
   left_method  => 'foo',
   right_class  => 'Bar',
   right_method => 'bar',
};

periodt. πŸ’œ


πŸ‘‘ METHODS πŸ‘‘

all these methods take a configuration hashref, and the hashref looks exactly like this:

{
   left_class          => 'Foo',
   left_method         => 'foo',     # see NOTE
   left_method_plural  => 'foos',    # see NOTE, not required, used for
                                     # many_to_many rel name in right_class
                                     # which is not generated by default
   right_class         => 'Bar',
   right_method        => 'bar',     # see NOTE
   right_method_plural => 'bars',    # see NOTE, not required, used for
                                     # many_to_many rel name in left_class
                                     # which is not generated by default
   namespace           => 'MyApp',   # default is guessed via *::Foo
   self_method         => 'foobars', # not required, used for setting the name of the
                                     # join table's relationship in a has_many
                                     # which is not generated by default
}

πŸŽ€ join_table

this is the method. the main character. 🌟 the one you probably actually want.

it will set your table, add columns, set the primary key, and set up the relationships. all four things. in one go. slay.


πŸŽ€ add_join_columns

adds two non-nullable integer fields, named ${left_method}_id and ${right_method}_id respectively. no notes. ✨


πŸŽ€ generate_has_manys

installs methods into left_class and right_class so you can get to the join table. those methods get named whatever you passed into the configuration hashref as self_method. πŸ’«


πŸŽ€ generate_many_to_manys

installs many_to_many methods into left_class and right_class. the names come from your config hashref: left_method_plural gives the name for the right_class, and right_method_plural gives the name for the left_class. yes, it's crossed over like that, and yes, that's intentional. πŸ”₯


πŸŽ€ generate_primary_key

sets ${left_method}_id and ${right_method}_id to be the primary key. two columns, one key, iconic. πŸ‘‘


πŸŽ€ generate_relationships

this adds relationships to ${namespace}::Schema::Result::$left_class and ${namespace}::Schema::Result::$left_class respectively.


πŸŽ€ set_table

this method sets the table to ${left_class}_${right_class}. simple as. πŸ“


🌈 CANDY EXPORTS 🌈

if you use this component together with DBIx::Class::Candy, it will export:

  • join_table
  • generate_primary_key
  • generate_has_manys
  • generate_many_to_manys
  • generate_relationships
  • set_table
  • add_join_columns

πŸ’€ NOTE

this module uses (an internal fork of) String::CamelCase to default the method names, and it uses Lingua::EN::Inflect for pluralization. iykyk. 🧚


πŸ¦‹ CHANGES BETWEEN RELEASES πŸ¦‹

⭐ Changes since 0.*

originally this module would use

data_type         => 'integer',
is_nullable       => 0,
is_numeric        => 1,

for all joining columns. now? it infers data_type, is_nullable, is_numeric, and extra from the foreign tables instead. glow-up fr fr. πŸ’…


πŸ’œ AUTHOR πŸ’œ

Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>


this software is copyright (c) 2024 by Arthur Axel "fREW" Schmidt.

this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


okay that's the whole page bestie, thanks for reading to the end 🫢 this was DBIx::...inTable(3pm) as shipped with perl v5.40.0, dated 2024-11-16. go forth and build your join tables, you've got this. πŸ’–βœ¨πŸŒΈ

πŸ“¦ where this page comes from