The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Catalyst::Model::RDBO - base class for Rose::DB::Object model

SYNOPSIS

 package MyApp::Model::Foo;
 use base qw( Catalyst::Model::RDBO );
 
 __PACKAGE__->config(
        name      => 'My::Rose::Class',
        manager   => 'My::Rose::Class::Manager',
        load_with => ['bar']
        );

 1;
 
 # then in your controller
 
 my $object = $c->model('Foo')->fetch(id=>1);
 
 

DESCRIPTION

Catalyst Model base class.

METHODS

new

Initializes the Model. This method is called by the Catalyst setup() method.

name

Returns the name value from config().

manager

Returns the manager value from config().

If manager is not defined in config(), the new() method will attempt to load a class named with the name value from config() with ::Manager appended. This assumes the namespace convention of Rose::DB::Object::Manager.

If there is no such module in your @INC path, then the fall-back default is Rose::DB::Object::Manager.

create( @params )

Returns new instance of the RDBO object, instantiated with @params. Same as calling:

 MyObject->new( @params );

Returns undef if there was any error creating the object. Check the context's error() method for any error message. Example:

 my $obj = $c->model('Object')->create( id => 100 );
 if (!$obj or @{$c->error})
 {
     # handle error
     # ...
 }

The method is called create() instead of new() because new() is a reserved method name in Catalyst::Model subclasses.

fetch( @params )

If present, @params is passed directly to name()'s new() method, and is expected to be an array of key/value pairs. Then the load() method is called on the resulting object.

If @params are not present, the new() object is simply returned, which is equivalent to calling create().

All the methods called within fetch() are wrapped in an eval() and sanity checked afterwards. If there are any errors, throw_error() is called.

Example:

 my $foo = $c->model('Foo')->fetch( id => 1234 );
 if (@{ $c->error })
 {
    # do something to deal with the error
 }
 

NOTE: If the object's presence in the database is questionable, your controller code may want to use create() and then call load() yourself with the speculative flag. Example:

 my $foo = $c->model('Foo')->create( id => 1234 );
 $foo->load(speculative => 1);
 if ($foo->not_found)
 {
   # do something
 }

fetch_all( @params )

Alias for search().

all( @params )

Alias for search().

search( @params )

@params is passed directly to the Manager get_objects() method. See the Rose::DB::Object::Manager documentation.

count( @params )

@params is passed directly to the Manager get_objects_count() method. See the Rose::DB::Object::Manager documentation.

iterator( @params )

@params is passed directly to the Manager get_objects_iterator() method. See the Rose::DB::Object::Manager documentation.

throw_error( msg )

Throws Catalyst::Exception. Override to manage errors in some other way.

NOTE that if in your subclass throw_error() is not fatal and instead returns a false a value, methods that call it will, be default, continue processing instead of returning. See fetch() for an example.

AUTHOR

Peter Karman

CREDITS

Thanks to Atomic Learning, Inc for sponsoring the development of this module.

Thanks to Bill Moseley for API suggestions.

LICENSE

This library is free software. You may redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Rose::DB::Object