From: Nigel Metheringham Date: Thu, 7 Sep 2006 16:30:30 +0000 (+0000) Subject: Added FAQ/Cookbook entry on Stringification. Contributed by Marcello Romani. X-Git-Tag: v0.07002~10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=462bb8478d9fdfb1b688495c51603ba2901053ef;p=dbsrgits%2FDBIx-Class.git Added FAQ/Cookbook entry on Stringification. Contributed by Marcello Romani. --- diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index a0bce5e..b18fb24 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -527,10 +527,43 @@ just looking for this. =head2 Stringification Employ the standard stringification technique by using the C -module. Replace C with the column/method of your choice. +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 '""' => 'foo', fallback => 1; +For more complex stringification, you can use an anonymous subroutine: + + use overload '""' => sub { $_[0]->name . ", " . + $_[0]->address }, fallback => 1; + +=head3 Stringifcation Example + +Suppose we have two tables: C and C. The table +specifications are: + + Product(id, Description, category) + Category(id, Description) + +C is a foreign key into the Category table. + +If you have a Product object C<$obj> and write something like + + print $obj->category + +things will not work as expected. + +To obtain, for example, the category description, you should add this +method to the class defining the Category table: + + use overload "" => sub { + my $self = shift; + + return $self->Description; + } + =head2 Disconnecting cleanly If you find yourself quitting an app with Control-C a lot during diff --git a/lib/DBIx/Class/Manual/FAQ.pod b/lib/DBIx/Class/Manual/FAQ.pod index 8b8baed..0c2ba53 100644 --- a/lib/DBIx/Class/Manual/FAQ.pod +++ b/lib/DBIx/Class/Manual/FAQ.pod @@ -324,3 +324,15 @@ search again or relationship accessors. The SQL query is only run when you ask the resultset for an actual row object. =back + +=head2 Notes for CDBI users + +=over 4 + +=item Is there a way to make an object auto-stringify itself as a +particular column or group of columns (a-la cdbi Stringfy column +group, or stringify_self method) ? + +See L + +=back