[Catalyst] Handling Exceptions

Kiefer, Sascha sk at intertivity.com
Sun Apr 16 20:31:16 CEST 2006


Okay.
But then, i have to do the rollback myself, right?

my $model       = $c->model('DBIC::PbUser');
my $create_user = sub {
    my $user =
      $model->create(
        { username => $username, password => $csh->generate } );
    $user->create_related( 'profile', { email => $emailaddr } );
    die "Just for testing";
    $user->create_related( 'settings',
      { registerDate => DateTime->now } );
};

my $rs;
eval { $rs = $model->schema_instance->	txn_do($create_user); };
if ($@) {
# ...
}

And now, i have a new in entry in my user and in my user_profile table.

Greetings,
--esskar

> -----Original Message-----
> From: Matt S Trout [mailto:dbix-class at trout.me.uk] 
> Sent: Sonntag, 16. April 2006 19:16
> To: sk at intertivity.com; The elegant MVC web framework
> Subject: Re: [Catalyst] Handling Exceptions
> 
> 
> Kiefer, Sascha wrote:
> > Hi.
> > 
> > First, see what i have:
> > 
> > package MyApp::Model::DBIC::User;
> > 
> > use strict;
> > use warnings;
> > use base 'DBIx::Class::Core';
> > 
> > __PACKAGE__->has_one ( profile  => 
> 'MyApp::Model::DBIC::UserProfile',
> > 'userId' );
> > __PACKAGE__->has_one ( settings => 
> 'MyApp::Model::DBIC::UserSettings',
> > 'userId' );
> > 
> > Now i create a user:
> > 
> > my $user = $c->model('DBIC::User')->create(
> > 	{
> > 		username => $username,
> > 		password => $csh->generate,
> > 	}
> > );
> > my $profile  = $user->create_related( 'profile',  { email => 
> > $emailaddr, } ); my $settings = $user->create_related( 
> 'settings', { 
> > registerDate =>
> > DateTime->now } );
> > 
> > 
> > This is all good, but let's say createing the settings does not 
> > work/fails for some reason. I get a exception. But then i 
> would like 
> > to rollback the creating of the user and ist profile.
> > What's the best practice for that?
> 
> perldoc DBIx::Class::Schema and read up on txn_do
> 
> -- 
>       Matt S Trout       Offering custom development, 
> consultancy and support
>    Technical Director    contracts for Catalyst, DBIx::Class 
> and BAST. Contact
> Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for 
> more information
> 
> + Help us build a better perl ORM: 
> + http://dbix-class.shadowcatsystems.co.uk/ +
> 




More information about the Catalyst mailing list