FAQ
Hi,

I want to place some database queries in App/lib/App/Modell/App.pm. How
can I access the DBI database handle in that module? Is that the right
place for complex queries?

The database tables are being loaded dynamically by
Catalyst::Model::DBIC::Schema/ DBIx::Class::Schema::Loader. I read that I
can get the DBH via $schema->storage->dbh -- but where do I get $schema
from?

Thanks for your help,

Stephan

Search Discussions

  • Mario Minati at Mar 21, 2007 at 11:43 am

    Am Mittwoch 21 M?rz 2007 11:39 schrieb Stephan Austerm?hle:
    Hi,

    I want to place some database queries in App/lib/App/Modell/App.pm. How
    can I access the DBI database handle in that module? Is that the right
    place for complex queries?
    Are you sure that your structure is right?
    Mine is (based on the Tutorial) App/lib/AppDB/Modell/Table.pm
    The database tables are being loaded dynamically by
    Catalyst::Model::DBIC::Schema/ DBIx::Class::Schema::Loader. I read that I
    can get the DBH via $schema->storage->dbh -- but where do I get $schema
    from?
    In the table class I can create custom functions to alter or get the
    resultsets.

    How complex can a query be that you want the DBI handle?
    With DBIC you can do almost everything, e.g. multi table joins, creating
    aliases with user defined functions and procedures.
    At least for me that's all I want :)

    Greets,
    Mario Minati
  • Stephan Austermühle at Mar 21, 2007 at 1:01 pm
    Hi,
    Are you sure that your structure is right?
    Mine is (based on the Tutorial) App/lib/AppDB/Modell/Table.pm
    I have named the AppDB like the application because the database is named
    that way. The file App.pm just contains

    __PACKAGE__->config(
    schema_class => 'App::Schema',
    connect_info => [
    'dbi:mysql:xxx',
    'xxx',
    'xxx',
    ],
    );

    Anyway, all tables are dynamically loaded so I assume that configuration
    is correct for the moment.
    In the table class I can create custom functions to alter or get the
    resultsets.
    Where do you put queries that join multiple tables? I think such queries
    do not fit well into a Table.pm but in AppDB.pm.
    With DBIC you can do almost everything, e.g. multi table joins, creating
    aliases with user defined functions and procedures.
    May be you can do anything with DBIC -- at the cost of having to learn it
    instead of just and easy placing an SQL statement into the code. Maybe
    DBIC would be more fun if I would have found better documentation. That is
    why I need to access a dbh.

    Regards, Stephan
  • Mario Minati at Mar 21, 2007 at 1:52 pm

    Am Mittwoch 21 M?rz 2007 14:01 schrieb Stephan Austerm?hle:
    Hi,
    Are you sure that your structure is right?
    Mine is (based on the Tutorial) App/lib/AppDB/Modell/Table.pm
    I have named the AppDB like the application because the database is named
    that way. The file App.pm just contains

    __PACKAGE__->config(
    schema_class => 'App::Schema',
    connect_info => [
    'dbi:mysql:xxx',
    'xxx',
    'xxx',
    ],
    );

    Anyway, all tables are dynamically loaded so I assume that configuration
    is correct for the moment.
    In the table class I can create custom functions to alter or get the
    resultsets.
    Where do you put queries that join multiple tables? I think such queries
    do not fit well into a Table.pm but in AppDB.pm.
    In my app they are, but I think we have a different setup.
    If you are interested I can post on of my table classes.
    With DBIC you can do almost everything, e.g. multi table joins, creating
    aliases with user defined functions and procedures.
    May be you can do anything with DBIC -- at the cost of having to learn it
    instead of just and easy placing an SQL statement into the code. Maybe
    DBIC would be more fun if I would have found better documentation. That is
    why I need to access a dbh.
    What about the Manual part of the docs, especially the Cookbook helped me a
    lot:
    http://search.cpan.org/~jrobinson/DBIx-Class-0.07005/lib/DBIx/Class/Manual/Cookbook.pod

    Greets,
    Maro Minati
  • Jason Kohles at Mar 21, 2007 at 1:04 pm

    On Mar 21, 2007, at 7:43 AM, Mario Minati wrote:

    Am Mittwoch 21 M?rz 2007 11:39 schrieb Stephan Austerm?hle:
    The database tables are being loaded dynamically by
    Catalyst::Model::DBIC::Schema/ DBIx::Class::Schema::Loader. I read
    that I
    can get the DBH via $schema->storage->dbh -- but where do I get
    $schema
    from?
    my $dbh = $c->model( 'MyModel' )->schema->storage->dbh;

    Just be sure you don't hold onto the handle for too long, or strange
    things can happen (especially in a persistent environment).
    In the table class I can create custom functions to alter or get the
    resultsets.

    How complex can a query be that you want the DBI handle?
    With DBIC you can do almost everything, e.g. multi table joins,
    creating
    aliases with user defined functions and procedures.
    At least for me that's all I want :)
    There is at least one thing that I've used this for that DBIx::Class
    can't do (or at least I couldn't figure out how at the time I needed
    to do it)...

    $c->model( 'MyModel' )->schema->storage->dbh->do( "NOTIFY
    update_listener" );

    --
    Jason Kohles
    email@jasonkohles.com
    http://www.jasonkohles.com/
    "A witty saying proves nothing." -- Voltaire
  • Stephan Austermühle at Mar 21, 2007 at 1:29 pm
    Hi,
    my $dbh = $c->model( 'MyModel' )->schema->storage->dbh;
    Thanks! Am I right with that this will work in Controllers only?

    Regards, Stephan

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedMar 21, '07 at 10:39a
activeMar 21, '07 at 1:52p
posts6
users3
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2022 Grokbase