[Catalyst] Best practices for extending DBIC model

Eric B. Wright ewright at linescale.com
Thu Jul 14 15:16:23 GMT 2011


Ah yes… sorry - since I never used DBIC before I used Catalyst I sometimes forget they are two different things! Thanks so much for your input. 

You have answered my question fabulously as I see now what I really want to do is totally possible. I just need to rework my namespacing a bit. (I wrote a simple script using DBIx::Class::Schema::Loader which needs some tweaking as it is putting all my Result classes under one monolithic namespace... although it does save me the hassle of manually creating my schema objects.)

I'll also explore using Traits.

Thanks again ninjas!

Best,
Eric

On Jul 14, 2011, at 10:28 AM, <Ian.Docherty at nomura.com> wrote:

> Eric
> First of all this is a Catalyst mailing list, not really a DBIC mailing list. :)
> 
> However, if I understand your problem. Assuming you have created your DBIC schema outside of Catalyst, you will have classes something like MyApp::DB::Result::Clients and MyApp::DB::ResultSet::Clients
> 
> The MyApp::DB::ResultSet::Clients is where you create your code that does your grouped_clients method. E.g.
> 
> ---------
> package MyApp::DB::ResultSet::Clients;
> 
> sub grouped_clients {
>    my ($self, $args) = @_;
> 
>    my @all_clients = $self->search($args);
>    for my $client (@clients) {
>        # massage your $client here
>        ...
>    }
>    return @all_clients;
> }
> 
> ---------
> 
> Then you just call $c->model('DB::Clients')->grouped_clients;
> 
> Or have I missed your point?
> 
> Regards
> Ian
> 
> 
> 
> 
> From: Eric Wright [mailto:ewright at linescale.com] 
> Sent: 14 July 2011 14:59
> To: The elegant MVC web framework
> Subject: [Catalyst] Best practices for extending DBIC model
> 
> Any input/comments on best practices for extending a DBIC model? I have a DBIC schema that for the most part does what I need it to do. I find where I need model specific enhancements I can easily extend the schema objects themselves keeping the data centric code out of my controller layer. What I'm struggling with is how best to handle things at a higher level - i.e. modifying the resultsets themselves. 
> 
> e.g. say I have a Clients table and I want to get *all* clients in the system and then massage them in some way. I cannot do this with simple SQL or DBIC joins. I can extend Clients.pm for level stuff but what I really need is a way to extend the DBIx::Class::ResultSet object that is returned by $c->model('DB::Clients') so that that I can perform something like the following:
> 
> my @clients = $c->model('DB::Clients')->grouped_clients;
> 
> I'm using Catalyst::Model::DBIC::Schema. Is there a preferred way to hook into the result set to customize things the way I need?
> 
> Or do I just create a new model altogether and call the schema from that model?
> 
> my @clients = $c->model('MyNewModel')->grouped_clients;
> 
> I dunno. The second method just seems a little kludgey to me unless I make it a complete wrapper around my DBIC schema. Any insight into how you guys build your apps would be most appreciated. 
> 
> Thanks!
> Eric
> 
> 
> 
> This e-mail (including any attachments) is confidential, may contain
> proprietary or privileged information and is intended for the named
> recipient(s) only. Unintended recipients are prohibited from taking action
> on the basis of information in this e-mail and must delete all copies.
> Nomura will not accept responsibility or liability for the accuracy or
> completeness of, or the presence of any virus or disabling code in, this
> e-mail. If verification is sought please request a hard copy. Any reference
> to the terms of executed transactions should be treated as preliminary only
> and subject to formal written confirmation by Nomura. Nomura reserves the
> right to monitor e-mail communications through its networks (in accordance
> with applicable laws). No confidentiality or privilege is waived or lost by
> Nomura by any mistransmission of this e-mail. Any reference to "Nomura" is
> a reference to any entity in the Nomura Holdings, Inc. group. Please read
> our Electronic Communications Legal Notice which forms part of this e-mail:
> http://www.Nomura.com/email_disclaimer.htm
> 
> 
> _______________________________________________
> List: Catalyst at 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/




More information about the Catalyst mailing list