Cédric Bouvier > Catalyst-Model-DBIDM-0.04 > Catalyst::Model::DBIDM



Annotate this POD


Open  0
View/Report Bugs
Module Version: 0.04   Source  


Catalyst::Model::DBIDM - DBIx::DataModel model class


Version 0.04


  1. Create the DBIx::DataModel schema in MyApp/Schema.pm:
        package MyApp::Schema;
        use DBIx::DataModel;
        MyApp::DM->Table(qw/ MyApp::DM::Employee   employee   emp_id /);
        MyApp::DM->Table(qw/ MyApp::DM::Department department dpt_id /);

    Notice that the DBIx::DataModel schema is MyApp::DM, not MyApp::Model::DM. It is usable as a standalone schema, without the need for Catalyst. In fact, it does not even need to be in the MyApp namespace.

  2. To expose it to Catalyst as a model, create a DM model in MyApp/Model/DM.pm:
        package MyApp::Model::DM;
        use base qw/ Catalyst::Model::DBIDM /;
        use MyApp::Schema; # to create the classes MyApp::DM
            schema_class => 'MyApp::DM',
            connect_info => [
                { RaiseError => 1 },

Now you have a working model, bound to your DBIx::DataModel schema, that can be accessed the Catalyst way, using $c->model().

    my $employee = $c->model('DM::Employee')->fetch(1);

$c->model('DM') merely returns the string "MyApp::DM", i.e., the name of the DBIx::DataModel schema, but it also ensures that it is connected to the database, as configured in the connect_info configuration entry.

$c->model('DM::Employee') (or any other table declared in MyApp::Schema) does the same (returns the string "MyApp::DM::Employee", and connects to the database if need be).




Constructor. It creates an ACCEPT_CONTEXT method for the MyApp::Model::DM class and for pseudo-classes for each of the schema's tables (MyApp::Model::DM::Employee, MyApp::Model::DM::Department, etc.)

This allows the Catalyst application to invoke $c->model('DM::Employee') prior to calling $c->model('DM') and still have the schema initialised properly.

The ACCEPT_CONTEXT methods invoke the connect_if_not() method before returning the Schema class name or the Table class name:

    $c->model('DM');            # "MyApp::DM"
    $c->model('DM::Employee');  # "MyApp::DM::Employee"

The pseudo-classes name are elaborated as follows:

  1. Take the class name as returned by the Schema's tables() method (MyApp::DM::Employee).
  2. Remove the Schema class name (MyApp::DM::), if possible, which leaves Employee.
  3. Prepend with the Model class name (MyApp::Model::DM::), resulting in MyApp::Model::DM::Employee, which can be called with $c->model('DM::Employee').

The Table class names need not be in the same namespace as the Schema class: if the substitution at step 2 fails, the Model class name is prepended to the full Table class name.

    # In MyApp::Schema
    MyApp::DM->Table(qw/ Employee employee emp_id /);

    # In MyApp::Model::DM
    use base qw/ Catalyst::Model::DBIDM /;
        schema_class => 'MyApp::DM',

    # In some controller code
    $c->model('DM::Employee')->fetch(1);   # Employee->fetch(1);

Initialises the Schema (i.e., connects to the database), if not already done. This method returns immediately if the Schema class already has a dbh() (See DBIx::DataModel). If not, it creates a database handler by invoking DBI->connect with the connect_info parameters from the configuration.

This method receives $c and the Schema class name as arguments.


Catalyst::Manual, Catalyst::Helper::Model::DBIDM


Cedric Bouvier, <cbouvi at cpan.org>


Please report any bugs or feature requests to bug-catalyst-model-dbidm at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Model-DBIDM. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc Catalyst::Model::DBIDM

You can also look for information at:


Praises go to Laurent Dami for writing DBIx::DataModel, and to Brandon L Black, for writing Catalyst::Model::DBIC::Schema, a great source of inspiration.


Copyright 2007 Cedric Bouvier, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: