X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class.git;a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FManual%2FCookbook.pod;h=6c45d9bec1668cf149cfcc9352bda41e2c3a0e65;hp=bd92f4320e93cecdeb05bf79f0614209a770397d;hb=e9188247f020a63ab8b6280c9dcdcb0df5b5f0c1;hpb=90545b68b06c2d2d288d593462539fe45fff48a6 diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index bd92f43..6c45d9b 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -107,15 +107,12 @@ to access the returned value: ); # Equivalent SQL: - # SELECT name name, LENGTH( name ) + # SELECT name name, LENGTH( name ) name_length # FROM artist -Note that the C< as > attribute has absolutely nothing to with the sql -syntax C< SELECT foo AS bar > (see the documentation in -L). If your alias exists as a -column in your base class (i.e. it was added with C), you -just access it as normal. Our C class has a C column, so -we just use the C accessor: +If your alias exists as a column in your base class (i.e. it was added +with C), you just access it as normal. Our C +class has a C column, so we just use the C accessor: my $artist = $rs->first(); my $name = $artist->name(); @@ -142,7 +139,7 @@ any of your aliases using either of these: select => [ { distinct => [ $source->columns ] } ], - as => [ $source->columns ] # remember 'as' is not the same as SQL AS :-) + as => [ $source->columns ] } ); @@ -179,10 +176,6 @@ L supports C as follows: # LEFT JOIN cd cds ON ( cds.artist = me.artistid ) # GROUP BY name -Please see L documentation if you -are in any way unsure about the use of the attributes above (C< join ->, C< select >, C< as > and C< group_by >). - =head3 Predefined searches You can write your own L class by inheriting from it @@ -422,7 +415,7 @@ ways, the obvious one is to use search: {}, { select => [ { sum => 'Cost' } ], - as => [ 'total_cost' ], # remember this 'as' is for DBIx::Class::ResultSet not SQL + as => [ 'total_cost' ], } ); my $tc = $rs->first->get_column('total_cost'); @@ -533,7 +526,7 @@ in the future. =head2 Many-to-many relationships -This is straightforward using L: +This is straightforward using L: package My::DB; # ... set up connection ... @@ -593,7 +586,7 @@ module. To make an object stringify itself as a single column, use something like this (replace C with the column/method of your choice): - use overload '""' => sub { shift->name}, fallback => 1; + use overload '""' => 'foo', fallback => 1; For more complex stringification, you can use an anonymous subroutine: @@ -1129,43 +1122,4 @@ Just use C instead, then check C: # do whatever else you wanted if it was a new row } -=head3 Wrapping/overloading a column accessor - -Problem: Say you have a table "Camera" and want to associate a description -with each camera. For most cameras, you'll be able to generate the description from -the other columns. However, in a few special cases you may want to associate a -custom description with a camera. - -Solution: - -In your database schema, define a description field in the "Camera" table that -can contain text and null values. - -In DBIC, we'll overload the column accessor to provide a sane default if no -custom description is defined. The accessor will either return or generate the -description, depending on whether the field is null or not. - -First, in your "Camera" schema class, define the description field as follows: - - __PACKAGE__->add_columns(description => { accessor => '_description' }); - -Next, we'll define the accessor-wrapper subroutine: - - sub description { - my $self = shift; - - # If there is an update to the column, we'll let the original accessor - # deal with it. - return $self->_description(@_) if @_; - - # Fetch the column value. - my $description = $self->_description; - - # If there's something in the description field, then just return that. - return $description if defined $description && length $descripton; - - # Otherwise, generate a description. - return $self->generate_description; - } - =cut