FAQ
Hi,

I need to access the same database in more controller modules, using the
following line in every subroutine and every module:

my $schema = $c->model('DB');

Is it possible to define it only in one place and make it available in the
entire application?

Thanks.

Octavian

Search Discussions

  • Robert 'phaylon' Sedlacek at Dec 24, 2006 at 4:14 pm

    Octavian Rasnita said:

    I need to access the same database in more controller modules, using the
    following line in every subroutine and every module:

    my $schema = $c->model('DB');

    Is it possible to define it only in one place and make it available in the
    entire application?
    Sorry, I don't get it. $c->model() *is* available in the entire application.

    --
    # Robert 'phaylon' Sedlacek
    # Perl 5/Catalyst Developer in Hamburg, Germany
    { EMail => ' rs@474.at ', Web => ' http://474.at ' }
  • Octavian Rasnita at Dec 24, 2006 at 4:35 pm
    Yes, sorry, I haven't explained very well, and I think I have found a
    solution.
    I have a few databases which are similar. Most of the tables are the same,
    and in some of them I have more other tables or some tables redefined.

    I want to test the application using a certain database, and if all works
    right, I want to use it with another one, and I don't want to need creating
    another Catalyst application just because I have changed the database.

    If I write the name of the database in more modules it will be harder to
    change its name, but now I I think that I could use something like:

    $c->model($c->config->{db});

    and just change the name of the db in the application config.

    Octavian

    ----- Original Message -----
    From: "Robert 'phaylon' Sedlacek" <rs@474.at>
    To: "The elegant MVC web framework" <catalyst@lists.rawmode.org>
    Sent: Sunday, December 24, 2006 8:14 AM
    Subject: Re: [Catalyst] schema



    Octavian Rasnita said:
    I need to access the same database in more controller modules, using the
    following line in every subroutine and every module:

    my $schema = $c->model('DB');

    Is it possible to define it only in one place and make it available in the
    entire application?
    Sorry, I don't get it. $c->model() *is* available in the entire application.

    --
    # Robert 'phaylon' Sedlacek
    # Perl 5/Catalyst Developer in Hamburg, Germany
    { EMail => ' rs@474.at ', Web => ' http://474.at ' }


    _______________________________________________
    List: Catalyst@lists.rawmode.org
    Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
    Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
    Dev site: http://dev.catalyst.perl.org/
  • Robert 'phaylon' Sedlacek at Dec 24, 2006 at 4:50 pm

    Octavian Rasnita said:

    I want to test the application using a certain database, and if all works
    right, I want to use it with another one, and I don't want to need
    creating another Catalyst application just because I have changed the
    database.

    [...]

    and just change the name of the db in the application config.
    Umm, why don't you just have two config files with different DSNs to your
    schema? During development, my config has a "dbi:SQLite:..." DSN in it,
    and the production server uses a postgres one.

    --
    # Robert 'phaylon' Sedlacek
    # Perl 5/Catalyst Developer in Hamburg, Germany
    { EMail => ' rs@474.at ', Web => ' http://474.at ' }
  • Octavian Rasnita at Dec 24, 2006 at 8:10 pm
    From: "Robert 'phaylon' Sedlacek" <rs@474.at>
    Umm, why don't you just have two config files with different DSNs to your
    schema? During development, my config has a "dbi:SQLite:..." DSN in it,
    and the production server uses a postgres one.
    Finally I think I will need to use such a config file, although the yaml
    format is not very friendly. I have fought a little with it, because the
    config file was saved with a 3-chars BOM, and the whole application refused
    starting, with an error message that wasn't very clear.
    I haven't used YAML until now, and I didn't know that the UTF-8 encoded
    config file must not have a BOM.

    Octavian
  • Robert 'phaylon' Sedlacek at Dec 24, 2006 at 8:31 pm

    Octavian Rasnita said:

    Finally I think I will need to use such a config file, although the yaml
    format is not very friendly.
    From the Catalyst::Plugin::ConfigLoader POD:
    This module will attempt to load find and load a configuration
    file of various types. Currently it supports YAML, JSON, XML,
    INI and Perl formats.

    --
    # Robert 'phaylon' Sedlacek
    # Perl 5/Catalyst Developer in Hamburg, Germany
    { EMail => ' rs@474.at ', Web => ' http://474.at ' }
  • Jonas Alves at Dec 24, 2006 at 4:56 pm

    If I write the name of the database in more modules it will be harder to
    change its name, but now I I think that I could use something like:

    $c->model($c->config->{db});

    and just change the name of the db in the application config.
    Or you can just use $c->model() and define the "default_model"
    parameter in the config file.

    --
    Jonas
  • Octavian Rasnita at Dec 24, 2006 at 8:10 pm
    From: "Jonas Alves" <jonas.alves@gmail.com>
    Or you can just use $c->model() and define the "default_model"
    parameter in the config file.

    --
    Jonas
    That's it! Thank you.

    Teddy
  • Robert 'phaylon' Sedlacek at Dec 24, 2006 at 8:33 pm

    Octavian Rasnita said:
    From: "Jonas Alves" <jonas.alves@gmail.com>
    Or you can just use $c->model() and define the "default_model"
    parameter in the config file.
    That's it! Thank you.
    Beware though that this probably means you can't access a DBIC resultset
    directly via $c->model('DB::Foo');

    --
    # Robert 'phaylon' Sedlacek
    # Perl 5/Catalyst Developer in Hamburg, Germany
    { EMail => ' rs@474.at ', Web => ' http://474.at ' }
  • Octavian Rasnita at Dec 24, 2006 at 8:39 pm

    Beware though that this probably means you can't access a DBIC resultset
    directly via $c->model('DB::Foo');
    Ok, but how?

    Thanks.

    Octavian
  • Robert 'phaylon' Sedlacek at Dec 24, 2006 at 10:02 pm

    Octavian Rasnita said:

    Beware though that this probably means you can't access a DBIC resultset
    directly via $c->model('DB::Foo');
    Ok, but how?
    Um, how _what_? It would be very helpful if you'd write whole questions,
    not assuming that I know what is on your mind. We both seem to be
    non-native english speakers and it would surely make this conversation
    easier for us both.

    Anyway, I'll just elaborate my statement:

    While you can access your DBIC schema through $c->model('DB'), The
    C:Model::DBIC::Schema provides you with a shortcut, because most of the
    time you want a resultset, not the schema. This allows you to do, for
    example, $c->model('DB::Person') to retrieve the 'Person' resultset in the
    'DB' schema. This means you can do

    my $barneys = $c->model('DB::Person')->search({name => 'barney'});

    instead of

    my $schema = $c->model('DB');
    my $barnes = $schema->resultset('Person')->search({name => 'barney'});

    And I don't think Model::DBIC::Schema pays attention to the default_model
    setting.

    --
    # Robert 'phaylon' Sedlacek
    # Perl 5/Catalyst Developer in Hamburg, Germany
    { EMail => ' rs@474.at ', Web => ' http://474.at ' }
  • Jonas Alves at Dec 26, 2006 at 11:31 am

    my $barneys = $c->model('DB::Person')->search({name => 'barney'});

    instead of

    my $schema = $c->model('DB');
    my $barnes = $schema->resultset('Person')->search({name => 'barney'});

    And I don't think Model::DBIC::Schema pays attention to the default_model
    setting.
    He can use the default_model setting and use
    $c->model->resultset('Person') to get the Person resultset.

    --
    Jonas

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedDec 24, '06 at 3:48p
activeDec 26, '06 at 11:31a
posts12
users3
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2022 Grokbase