[Catalyst] getting database handles and run arbitrary SQL
Tomas Doran
bobtfish at bobtfish.net
Wed Sep 24 09:34:04 BST 2008
On 24 Sep 2008, at 08:37, Johannes Plunien wrote:
> On 24.09.2008, at 09:15, theshz wrote:
>
>> Is it possible to get the database handle and run SQL directly as
>> in DBI?
>
> yes, try:
>
> my $dbh = $c->model('DBIC::User')->result_source->storage->dbh;
However - please note that doing this in your Controller is _not_ a
recommended application pattern.
If you do that, you will swiftly have domain logic in your
controllers, and any useful testing you can do will be forced to be
system testing with WWW::Mechanize. (There is nothing to test in the
model, and mocking the whole of Catalyst + Model to test is just too
much).
I highly recommend that you implement your domain logic in a model
class (it doesn't matter if that is a DBIC class, or a class with
methods that work on a $dbh - see Catalyst::Model::DBI, or anything
else).
Then your controller code becomes a case of marshaling some request
parameters, throwing them into your model and stashing the resultant
data structures produced by the model. Controller means that it
controls the other objects actions, *not* that it does the heavy
lifting itself.
You can then reuse your domain logic outside of Catalyst and test it
in isolation (and trivially write command line scripts which also
interact with the model), and the controllers are simple enough that
testing them using mock objects is doable.
Cheers
t0m
More information about the Catalyst
mailing list