[Catalyst] Reading Database At Startup

Eden Cardim edencardim at gmail.com
Wed Aug 19 11:44:02 GMT 2009


On Sun, Aug 16, 2009 at 12:09 PM, Ovid<publiustemp-catalyst at yahoo.com> wrote:
> Hi all,
>
> This should be painfully obvious, but I don't see it :)
>
> For a personal project, I want users to be able to click on a letter and get a list of countries starting with that letter.  I can do this:
>
>    my $letters = $c->model('DB')->storage->dbh->selectcol_arrayref(
>        'select distinct(substr(name,1,1)) as letter from country order by letter'
>    );
>    $c->stash->{letters} = $letters;
>
>
> But the country list is static and I want this available at startup.  'sub begin' fails because that's called once per request, not once per app.
>
> What's the recommend way of handling this?  (I'm using DBIx::Class for the model)

package MyApp::Model::DB;

extends 'Catalyst::Model::DBIC::Schema';

has country_letters => (isa => 'ArrayRef', is => 'ro', lazy_build => 1);

sub _build_country_letters {
  my($self) = @_;
  return $self->storage->dbh->selectcol_arrayref(
    'select distinct(substr(name,1,1)) as letter from country order by letter'
  );
};

sub BUILD { shift->country_letters } # optionally, force load-time construction

# in a distant controller

$c->stash->{letters} = $c->model('DB')->country_letters;

-- 
   Eden Cardim       Need help with your Catalyst or DBIx::Class project?
  Code Monkey                    http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://edenc.vox.com/            http://www.shadowcat.co.uk/servers/



More information about the Catalyst mailing list