[Dbix-class] filter many_to_many link table rows on foreign column values

Peter Rabbitson rabbit+dbic at rabbit.us
Wed Mar 30 12:27:42 GMT 2016


On 03/30/2016 02:23 PM, Peter Rabbitson wrote:
> perl -It/lib -Ilib -MANFANG -MDevel::Dwarn -e '
>
>    # add the special rel
>    require DBICTest::Schema::CD;
>    DBICTest::Schema::CD->has_many( links_to_non_mst_producers =>
> "DBICTest::Schema::CD_to_Producer", sub {
>      my $args = shift;
>      {
>        "$args->{foreign_alias}.cd" => { -ident =>
> "$args->{self_alias}.cdid" },
>        "$args->{foreign_alias}.producer" => {
>          -in => $args->{self_resultsource}
>                       ->schema
>                        ->resultset("Producer")
>                         ->search({ "me.name" => { -not_like => "%Trout%"
> } })
>                          ->get_column("producerid")
>                           ->as_query
>        },
>      }
>    });
>
>
>    require DBICTest;
>
>
>    Dwarn [ DBICTest->init_schema->resultset("CD")->search(
>      { cdid => 1 },
>      {
>        result_class => "DBIx::Class::ResultClass::HashRefInflator",
>        prefetch => {
>          cd_to_producer => "producer",
>          links_to_non_mst_producers => "producer",
>        }
>      }
>    )->all ];
> '

Forgot to add - this is what the above produces:

[
   {
     artist => 1,
     cd_to_producer => [
       {
         attribute => undef,
         cd => 1,
         producer => {
           name => "Matt S Trout",
           producerid => 1
         }
       },
       {
         attribute => undef,
         cd => 1,
         producer => {
           name => "Bob The Builder",
           producerid => 2
         }
       },
       {
         attribute => undef,
         cd => 1,
         producer => {
           name => "Fred The Phenotype",
           producerid => 3
         }
       }
     ],
     cdid => 1,
     genreid => 1,
     links_to_non_mst_producers => [
       {
         attribute => undef,
         cd => 1,
         producer => {
           name => "Bob The Builder",
           producerid => 2
         }
       },
       {
         attribute => undef,
         cd => 1,
         producer => {
           name => "Fred The Phenotype",
           producerid => 3
         }
       }
     ],
     single_track => undef,
     title => "Spoonful of bees",
     year => 1999
   }
]




More information about the DBIx-Class mailing list