From: Jay Hannah Date: Thu, 21 Jan 2010 05:48:14 +0000 (+0000) Subject: Added FAQ: Custom methods in Result classes X-Git-Tag: v0.08116~41 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a14a46e22efe21c8c516de0c97ce13c42d1d8056;p=dbsrgits%2FDBIx-Class.git Added FAQ: Custom methods in Result classes --- diff --git a/Changes b/Changes index a62dd5c..f4fdf02 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,6 @@ Revision history for DBIx::Class + - FAQ "Custom methods in Result classes" - Perl 5.8.1 is now the minimum supported version - Subqueries no longer marked experimental - might_have/has_one now warn if applied calling class's column diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm index e34b47f..ad01126 100644 --- a/lib/DBIx/Class.pm +++ b/lib/DBIx/Class.pm @@ -267,6 +267,8 @@ jgoulah: John Goulah jguenther: Justin Guenther +jhannah: Jay Hannah + jnapiorkowski: John Napiorkowski jon: Jon Schutz diff --git a/lib/DBIx/Class/Manual/FAQ.pod b/lib/DBIx/Class/Manual/FAQ.pod index 7ec01da..7bced4c 100644 --- a/lib/DBIx/Class/Manual/FAQ.pod +++ b/lib/DBIx/Class/Manual/FAQ.pod @@ -433,6 +433,38 @@ data out. =back +=head2 Custom methods in Result classes + +You can add custom methods that do arbitrary things, even to unrelated tables. +For example, to provide a C<< $book->foo() >> method which searches the +cd table, you'd could add this to Book.pm: + + sub foo { + my ($self, $col_data) = @_; + return $self->result_source->schema->resultset('cd')->search($col_data); + } + +And invoke that on any Book Result object like so: + + my $rs = $book->foo({ title => 'Down to Earth' }); + +When two tables ARE related, L provides many +methods to find or create data in related tables for you. But if you want to +write your own methods, you can. + +For example, to provide a C<< $book->foo() >> method to manually implement +what create_related() from L does, you could +add this to Book.pm: + + sub foo { + my ($self, $relname, $col_data) = @_; + return $self->related_resultset($relname)->create($col_data); + } + +Invoked like this: + + my $author = $book->foo('author', { name => 'Fred' }); + =head2 Misc =over 4