[Dbix-class] Update All Related Tables

Nickolay Platonov nickolay8 at gmail.com
Fri Oct 17 21:57:17 BST 2008


You may try to write a function, which will recursively check the record and
call "update" on each subclass of DBIx::Class.

On Sat, Oct 18, 2008 at 12:35 AM, Jess Robinson <
castaway at desert-island.me.uk> wrote:

>
>
> 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" =
=3D>
>> "self.id" });
>>
>> Table: bills
>> package Schema::Bill;
>> __PACKAGE__->belongs_to("company", "Schema::Company", { id =3D> "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 =3D $app->schema->resultset('Bill')->search(
>>   {
>>       'me.id'     =3D> {'>', 0}
>>   },
>>   {
>>       join        =3D> [qw/company/],
>>       prefetch    =3D> [qw/company/],
>>   }
>> );
>>
>> my $record =3D $rs->next;
>>
>> print $record->bill_description;           # prints 'Bill for developmen=
t'
>> $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
>
>
>
> _______________________________________________
> 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/20081018/183=
c751f/attachment.htm


More information about the DBIx-Class mailing list