The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mango::Provider::DBIC - Provider class for DBIx::Class based providers

SYNOPSIS

    package MyApp::Provider::Users;
    use strict;
    use warnings;
    
    BEGIN {
        use base qw/Mango::Provider::DBIC/;
    };
    __PACKAGE__->schema_class('MySchema');
    __PACKAGE__->source_name('Users');
    
    my $object = $provider->create(\%data);

DESCRIPTION

Mango::Provider::DBIC is a base abstract class for all DBIx::Class based providers used in Mango.

CONSTRUCTOR

new

Arguments: \%options

Creates a new provider object. If options are passed to new, those are sent to setup.

    my $provider = Mango::Provider::DBIC->new({
        schema_class => 'MySchema',
        source_name  => 'Users',
        result_class => 'MyResultClass'
    });

The following options are available at the class level, to new/setup and take the same data as their method counterparts:

    connection_info
    resultset
    schema
    schema_class
    source_name

See "new" in Mango::Provider a list of other possible options.

METHODS

connection_info

Arguments: \@info

Gets/sets the connection information used when connecting to the database.

    $provider->connection_info(['dbi:mysql:foo', 'user', 'pass', {PrintError=>1}]);

The info argument is an array ref that holds the following values:

$dsn

The DBI dsn to use to connect to.

$username

The username for the database you are connecting to.

$password

The password for the database you are connecting to.

\%attr

The attributes to be pass to DBI for this connection.

See DBI for more information about dsns and connection attributes.

create

Arguments: \%data

Creates a new object of type result_class using the supplied data.

    my $object = $provider->create({
        id => 23,
        thingy => 'value'
    });

delete

Arguments: \%filter or $object

Deletes objects from the store matching the supplied filter.

    $provider->delete({
        col => 'value'
    });

It can also delete an object passed into it:

    $provider->delete($object);

This is the same as:

    $provider->delete({ id => $object->id });

get_by_id

Arguments: $id

Retrieves an object from the provider matching the specified id.

    my $object = $provider->get_by_id(23);

Returns undef if no matching result can be found.

resultset

Arguments: $resultset

Gets/sets the DBIx::Class::Resultset to be used by this provider. If no resultset is set, the resultset for the specified source_name will be created automatically.

    $provider->resultset;
    # same as $schema->resultset($provider->source_name)
    
    $provider->resultset(
        $schema->resultset($provder->source_name)->search({default => 'search'})
    );

schema

Arguments: $schema

Gets/sets the DBIx::Class schema instance to be used for this provider. If no schema is set, a new instance of the schema_class will be created automatically when it is needed.

    my $schema = $provider->schema;
    $schema->dbh->{'AutoCommit'} = 0;

schema_class

Arguments: $class

Gets/sets the DBIx::Class schema class to be used for this provider. An exception will be thrown if the specified class can not be loaded.

    $provider->schema_class('MySchema');
    my $schema = $provider->schema;
    print ref $schema; # MySchema

If no schema class is specified in the subclass, the default schema class is Mango::Schema.

Arguments: \%filter, \%options

Returns a list of objects in list context or a Mango::Iterator in scalar context matching the specified filter.

    my @objects = $provider->search({
        col => 'value'
    });
    
    my $iterator = $provider->search({
        col => 'value'
    });

See "ATTRIBUTES" in DBIx::Class::Resultset for a list of possible options.

source_name

Arguments: $source

Gets/sets the DBIx::Class schema source to be used when creating the default resultset.

    $provider->source_name('Users');
    $provider->resultset;
    ## same as $schema->resultset('Users')

update

Arguments: $object

Sets the 'updated' column to DateTime->now and saves any changes made to the object back to the underlying store.

    my $object = $provider->create(\%data);
    $object->col('value');
    
    $provider->update($object);

SEE ALSO

Mango::Provider, DBIx::Class

AUTHOR

    Christopher H. Laco
    CPAN ID: CLACO
    claco@chrislaco.com
    http://today.icantfocus.com/blog/