[Dbix-class] Mixins/roles with relations

Олег Пронин syber.rus at gmail.com
Fri Nov 6 16:45:50 GMT 2020


For example

package MyComponent;
use 5.012;

sub add_columns {
    my ($class, @cols) = @_;
    return $class->next::method(@cols, 'additional', 'cols');
}



Relationships are more complicated. As far as i remember you can't add it
from inside "add_columns".

However you could call some method in every RS when your app finishing
initialization and add relationship from there

    foreach my $rsname (qw/User UserExtra SocialAction SocialPayment/) {
        my $source = $general_schema_class->source($rsname) or next;
        $source->result_class->finalize_schema($source) if
$source->result_class->can('finalize_schema');
    }

....

  package MyComponent;

sub finalize_schema {
    my ($class, $source) = @_;

        $_->add_relationship('payments',
$source->schema->source('SocialPayment')->result_class, {'foreign.user' => '
self.id'}, {
             'accessor'       => 'multi',
             'cascade_copy'   => 1,
             'cascade_delete' => 0,
             'is_depends_on'  => 0,
             'join_type'      => 'LEFT',
        }) for $class, $source;
}

пт, 6 нояб. 2020 г. в 18:05, Robert Rothenberg <robrwo at gmail.com>:

> Is it possible to write a DBIC result class that loads a shared
> component or mixin/role that defines a column and relation, e.g.
> something like
>
> __PACKAGE__->add_column( "foobar_id",   { data_type => "integer",
> is_foreign_key => 1 } ); __PACKAGE__->belongs_to( "foobar",
> "My::Schema::Result::Foobar", { "id" => "foobar_id" } ); so that this
> code can be reused in other results? I've experimented with treating
> this as a component or parent class but get errors: Can't locate object
> method "result_source_instance" Is there a way to defer running
> "belongs_to" until after a result source is specified?
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive:
> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20201106/f331a1ed/attachment.htm>


More information about the DBIx-Class mailing list