FAQ
I have what I hope is not a dumb question. I'm trying to follow best
practices here and am writing a piece of business logic that I feel
should be Catalyst agnostic and therefore am creating a custom model.
(However, at the moment Catalyst is the primary consumer of this lib.)
I'd like to use my DBIC::Schema goodness for DB access and happily
have abstracted that via Catalyst::Model::DBIC::Schema. The thing is I
realized that if I connect to this schema outside of the scope of
Catalyst via DBIx::Class::Schema->connect I have to pass in the
connection info separately which really bugs me because I've already
defined it via a configuration file for the Catalyst app (using
Config::General). I suppose I could just pass that info into the model
but that seems messy and redundant to me. If I subclass
Catalyst::Model well then I'm tightly coupled with Catalyst again. I'd
like to just be able to glue the model to Catalyst and do a:

$c->model('MyCustomModel')->foo(...)

and it just work.

It seems to me there should be a simple way for this schema to know
its connection info based on context and I figured there's like a
slick Catalyst ninja kind of method for doing this that my brain is
just not grasping at the moment. How do you do this sort of thing? Is
the magic in the Catalyst glue?

Thanks in advance for any advice/thoughts/comments...

-Eric

Search Discussions

  • Byron Young at Apr 10, 2009 at 7:42 pm

    Eric Wright wrote on 2009-04-10:
    I have what I hope is not a dumb question. I'm trying to follow best
    practices here and am writing a piece of business logic that I feel
    should be Catalyst agnostic and therefore am creating a custom model.
    (However, at the moment Catalyst is the primary consumer of this lib.)
    I'd like to use my DBIC::Schema goodness for DB access and happily have
    abstracted that via Catalyst::Model::DBIC::Schema. The thing is I
    realized that if I connect to this schema outside of the scope of
    Catalyst via DBIx::Class::Schema->connect I have to pass in the
    connection info separately which really bugs me because I've already
    defined it via a configuration file for the Catalyst app (using
    Config::General). I suppose I could just pass that info into the model
    but that seems messy and redundant to me. If I subclass Catalyst::Model
    well then I'm tightly coupled with Catalyst again. I'd like to just be
    able to glue the model to Catalyst and do a:

    $c->model('MyCustomModel')->foo(...)

    and it just work.

    It seems to me there should be a simple way for this schema to know its
    connection info based on context and I figured there's like a slick
    Catalyst ninja kind of method for doing this that my brain is just not
    grasping at the moment. How do you do this sort of thing? Is the magic
    in the Catalyst glue?

    Thanks in advance for any advice/thoughts/comments...

    -Eric
    I'm not sure if there's a special Catalyst way of doing this, but I use my schema classes from my various places, so I moved my db connection data into a separate config file that all apps can access. I have a separate class I wrote that looks for the config file in a few specific places (depending on whether you're in dev mode or production mode) and reads it. This class provides the connection info to the schema class. This way I can easily move my database, update the info in one file, and all apps automatically get the new connection info.

    Hope that helps.

    Byron
  • Ivan at Apr 13, 2009 at 10:54 am
    If I use the custom fields ( sub field1 ... ) in some dynamic tables -
    whether it is possible to describe this fields in a separate place for
    dynamic tables?
  • Rodrigo de Oliveira at Apr 10, 2009 at 7:50 pm

    The thing is I
    realized that if I connect to this schema outside of the scope of
    Catalyst via DBIx::Class::Schema->connect I have to pass in the
    connection info separately which really bugs me because I've already
    defined it via a configuration file for the Catalyst app (using
    Config::General). I suppose I could just pass that info into the model
    but that seems messy and redundant to me.

    To me it doesn't seem messy to parse myapp.conf from your non-catalyst app
    using Config::General->getall, then pass the connect_info data to your
    DBIC::Schema class. Besides, you can use your MyApp::Schema classes from
    anywhere, in case you created them with DBIC::Schema::Loader.
    My 2c.

    -rodrigo
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20090410/33e7e516/attachment.htm
  • Ian Docherty at Apr 11, 2009 at 9:17 am

    Eric Wright wrote:
    I have what I hope is not a dumb question. I'm trying to follow best
    practices here and am writing a piece of business logic that I feel
    should be Catalyst agnostic and therefore am creating a custom model.
    (However, at the moment Catalyst is the primary consumer of this lib.)
    I'd like to use my DBIC::Schema goodness for DB access and happily
    have abstracted that via Catalyst::Model::DBIC::Schema. The thing is I
    realized that if I connect to this schema outside of the scope of
    Catalyst via DBIx::Class::Schema->connect I have to pass in the
    connection info separately which really bugs me because I've already
    defined it via a configuration file for the Catalyst app (using
    Config::General). I suppose I could just pass that info into the model
    but that seems messy and redundant to me. If I subclass
    Catalyst::Model well then I'm tightly coupled with Catalyst again. I'd
    like to just be able to glue the model to Catalyst and do a:

    $c->model('MyCustomModel')->foo(...)

    and it just work.

    It seems to me there should be a simple way for this schema to know
    its connection info based on context and I figured there's like a
    slick Catalyst ninja kind of method for doing this that my brain is
    just not grasping at the moment. How do you do this sort of thing? Is
    the magic in the Catalyst glue?

    Thanks in advance for any advice/thoughts/comments...

    -Eric
    I use an external yaml file to hold connection information. In that way
    I can easily have different
    configurations for different environments, dev, staging, live.

    # production database access
    DBIxProd:
    dsn: 'DBI:mysql:host=localhost;database=myapp_prod'
    username: fred
    password: secret
    ...

    In my Catalyst model I do something like.

    package myapp::Model::DBIxProd;

    use strict;
    use base 'Catalyst::Model::DBIC::Schema';

    __PACKAGE__->config(
    schema_class => 'myapp::Storage',
    connect_info => [
    myapp->config->{DBIxProd}{dsn},
    myapp->config->{DBIxProd}{username},
    myapp->config->{DBIxProd}{password},
    { 'mysql_enable_utf8' => 1 },
    { on_connect_do =>[ 'set names utf8' ] },
    ]);

    1;

    And in my external app (usually in a 'tools' subdirectory) I do
    something like.

    use FindBin;
    use lib "$FindBin::Bin/../lib";

    use myapp::Storage;
    use YAML;

    my $config = YAML::LoadFile("$FindBin::Bin/../myapp.yml");

    my $schema = myapp::Storage->connect(
    $config->{DBIxProd}{dsn},
    $config->{DBIxProd}{username},
    $config->{DBIxProd}{password},
    { mysql_enable_utf8 => 1 },
    { on_connect_do => [ 'set names utf8' ] },
    );


    Regards
    Ian
  • Eric Wright at Apr 13, 2009 at 1:02 pm
    Thanks for your thoughts guys. These are all good ideas. I figure it's
    always good to do a sanity check. :)

    Best,
    Eric
    On Sat, Apr 11, 2009 at 5:17 AM, Ian Docherty wrote:
    Eric Wright wrote:
    I have what I hope is not a dumb question. I'm trying to follow best
    practices here and am writing a piece of business logic that I feel
    should be Catalyst agnostic and therefore am creating a custom model.
    (However, at the moment Catalyst is the primary consumer of this lib.)
    I'd like to use my DBIC::Schema goodness for DB access and happily
    have abstracted that via Catalyst::Model::DBIC::Schema. The thing is I
    realized that if I connect to this schema outside of the scope of
    Catalyst via DBIx::Class::Schema->connect I have to pass in the
    connection info separately which really bugs me because I've already
    defined it via a configuration file for the Catalyst app (using
    Config::General). I suppose I could just pass that info into the model
    but that seems messy and redundant to me. If I subclass
    Catalyst::Model well then I'm tightly coupled with Catalyst again. I'd
    like to just be able to glue the model to Catalyst and do a:

    $c->model('MyCustomModel')->foo(...)

    and it just work.

    It seems to me there should be a simple way for this schema to know
    its connection info based on context and I figured there's like a
    slick Catalyst ninja kind of method for doing this that my brain is
    just not grasping at the moment. How do you do this sort of thing? Is
    the magic in the Catalyst glue?

    Thanks in advance for any advice/thoughts/comments...

    -Eric
    I use an external yaml file to hold connection information. In that way I
    can easily have different
    configurations for different environments, dev, staging, live.

    # production database access
    DBIxProd:
    ? dsn: ? ? ? ? ? ?'DBI:mysql:host=localhost;database=myapp_prod'
    ? username: ? ? ? fred
    ? password: ? ? ? secret
    ...

    In my Catalyst model I do something like.

    package myapp::Model::DBIxProd;

    use strict;
    use base 'Catalyst::Model::DBIC::Schema';

    __PACKAGE__->config(
    ? schema_class ? ?=> 'myapp::Storage',
    ? connect_info => [
    ? ? ? myapp->config->{DBIxProd}{dsn},
    ? ? ? myapp->config->{DBIxProd}{username},
    ? ? ? myapp->config->{DBIxProd}{password},
    ? ? ? { 'mysql_enable_utf8' => 1 },
    ? ? ? { on_connect_do =>[ 'set names utf8' ] },
    ? ]);

    1;

    And in my external app (usually in a 'tools' subdirectory) I do something
    like.

    use FindBin;
    use lib "$FindBin::Bin/../lib";

    use myapp::Storage;
    use YAML;

    my $config = YAML::LoadFile("$FindBin::Bin/../myapp.yml");

    my $schema = myapp::Storage->connect(
    ? $config->{DBIxProd}{dsn},
    ? $config->{DBIxProd}{username},
    ? $config->{DBIxProd}{password},
    ? { mysql_enable_utf8 ?=> 1 },
    ? { on_connect_do => [ 'set names utf8' ] },
    );


    Regards
    Ian

    _______________________________________________
    List: Catalyst@lists.scsys.co.uk
    Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
    Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
    Dev site: http://dev.catalyst.perl.org/


    --
    -----------------------------------------------------------
    Eric Wright
    Rapid Synergy LLC
    Web Development, Software & Consulting
    V: 203.758.9270 F: 203.725.0853
    http://www.rapidsynergy.com
    -----------------------------------------------------------

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedApr 10, '09 at 6:59p
activeApr 13, '09 at 1:02p
posts6
users5
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2021 Grokbase