---
name: "DBIx::Class::Helper::Row::JoinTable"
section: "3pm"
platform: "linux"
package: "libdbix-class-helpers-perl"
version: "2.037000-1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/DBIx::Class::Helper::Row::JoinTable"
---

# DBIx::Class::Helper::Row::JoinTable(3pm)

Easily set up join tables with DBIx::Class

## NAME

DBIx::Class::Helper::Row::JoinTable - Easily set up join tables with DBIx::Class

## SYNOPSIS

```
 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');
```

or with DBIx::Class::Candy:

```
 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',
 };
```

## METHODS

All the methods take a configuration hashref that looks like the following:

```
 {
    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 that you probably want.  It will set your table, add columns, set the primary key, and set up the relationships.

### add\_join\_columns

Adds two non-nullable integer fields named `"${left_method}_id"` and `"${right_method}_id"` respectively.

### generate\_has\_manys

Installs methods into `"left_class"` and `"right_class"` to get to the join table. The methods will be named what's passed into the configuration hashref as `"self_method"`.

### generate\_many\_to\_manys

Installs many\_to\_many methods into `"left_class"` and `"right_class"`.  The methods will be named what's passed into the configuration hashref as `"left_method_plural"` for the `"right_class"` and `"right_method_plural"` for the `"left_class"`.

### generate\_primary\_key

Sets `"${left_method}_id"` and `"${right_method}_id"` to be the primary key.

### 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}".

## CANDY EXPORTS

If used in conjunction with DBIx::Class::Candy this component 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 uses Lingua::EN::Inflect for pluralization.

## CHANGES BETWEEN RELEASES

### Changes since 0.\*

Originally this module would use

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

for all joining columns.  It now infers `"data_type"`, `"is_nullable"`, `"is_numeric"`, and `"extra"` from the foreign tables.

## AUTHOR

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

## COPYRIGHT AND LICENSE

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.
