PrePAN

Sign in to PrePAN

DBIx::Lite::Model Subclass of DBIx::Lite for reusable models in YAML

Good

Synopsis

Create a yaml file. You may wish to have one model have the connection
    information, then you can create other models within that yaml file
    which use that model for their connection. An example is below.

        # mydb.yml
        MyDB:
            connect: Pg:dbname=some_db
            user: foo_user
            pass: foo_pass
    
        Client:
            model: MyDB
            table: clients
            primary_key: id
    
        Customer:
            model: MyDB
            table: customers
            primary_key: id
            related: clients <-> customers(client_id)


This allows us to do stuff like

        # test.pl
        use DBIx::Lite::Model;
    
        my $dbix = DBIx::Lite::Model->new;  # Creates a new instance
        $dbix->config('mydb.yml');          # load the configuration

        my $customer_rs = $dbix->model('Customer'); # Returns the Customer ResultSet
        my $client_rs   = $dbix->model('Client');   # Returns the Client ResultSet

        # we want to find out what customers client with id 5 has
        my $customers = $client_rs->find(5)->customers;
        $customers->single->name; # returns the name of the clients first customer

Description

DBIx::Lite is fantastic when you don't need the great features DBIx::Class offers. One thing I didn't want to do was continually enter the connection string, so DBIx::Lite::Model was born. You can store your connection string in a YAML file and create "ResultSet's" on from that model, so you only write the connection information once. Because your models are stored in a YAML file they can easily be reused in other code too.

Comments

Please sign up to post a review.