[Dbix-class] Update All Related Tables

Jess Robinson castaway at desert-island.me.uk
Fri Oct 17 21:35:30 BST 2008



On Thu, 16 Oct 2008, Ozum Eldogan wrote:

> I'm working on a simple bill recording system. A company may have many bills
> and I want to update both company and a related bill with a single update
> method call.
>
> Table: companies
> package Schema::Company;
> __PACKAGE__->has_many("bills", "Schema::Bill", { "foreign.company_id" =>
> "self.id" });
>
> Table: bills
> package Schema::Bill;
> __PACKAGE__->belongs_to("company", "Schema::Company", { id => "company_id"
> });
>
> For testing purposes I have only one company and several bills belongs to
> that company. In my code I get a company and all bills via
>
> my $rs = $app->schema->resultset('Bill')->search(
>    {
>        'me.id'     => {'>', 0}
>    },
>    {
>        join        => [qw/company/],
>        prefetch    => [qw/company/],
>    }
> );
>
> my $record = $rs->next;
>
> print $record->bill_description;           # prints 'Bill for development'
> $record->bill_description('Service Bill');
>
> print $record->company->name;              # prints 'Speed Inc.'
> $record->company->name('Slow Inc.');
>
> $record->update;                           # updates only bill
> $record->company->update;                  # updates company
>
>
> I want $record->update to update both bill and related company. In future I
> may have lots of related tables and I don't want to iterate through and hand
> code check and update all changed records.
>

SQL doesnt have a method to update two tables in one call..

You're looking for a transaction, see DBIx::Class::Schema "txn_do".

Jess




More information about the DBIx-Class mailing list