Matt S Trout ha scritto:
Marcello Romani wrote:
I have written a small paragraph that clarifies (I hope :-) why DBIC has no "stringify_self" method or "Stringify" column group.

This obviously intended for DBIC newcomers coming from CDBI...


------------------------------------------------------------------------

--- FAQ.pod     2006-08-18 13:00:27.000000000 +0200
+++ FAQ_modified.pod    2006-09-07 12:09:23.000000000 +0200
@@ -324,3 +324,43 @@
 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) ?
+
+Perl already offers a method to make an object stringify itself, through the 
C<use overload> pragma.
+
+Therefore there is no need for a specialized stringification method like 
C<stringify_self>.
+
+B<Quick example:>
+
+   use overload "" => sub { $_[0]->name . ", " . $_[0]->address }
+
+
+B<Scenario:>
+
+Suppose we have two tables: C<Product> and C<Category>. The table 
specifications are:
+
+  Product(id, Description, category)
+  Category(id, Description)
+
+C<category> 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 mapping the Category table (tipically C<DB::Main::Category>):
+
+  use overload "" => sub {
+      my $self = shift;
+
+      return $self->Description;
+  }
+
+=back

There's already a rather more minimal entry at

http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Manual/Cookbook.pod#Stringification

Maybe merge the better description into the cookbook and create a simpler FAQ entry that links to it?

Anybody got any better ideas?


I merged the two entries, expanding the cookbook entry and adding a pointer in the FAQ.

--
Marcello Romani
Responsabile IT
Ottotecnica s.r.l.
http://www.ottotecnica.com
--- FAQ.pod     2006-08-18 13:00:27.000000000 +0200
+++ FAQ_modified.pod    2006-09-07 18:06:37.000000000 +0200
@@ -324,3 +324,13 @@
 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<Cookbook/Stringification>
+
+=back
--- Cookbook.pod        2006-08-18 13:00:27.000000000 +0200
+++ Cookbook_modified.pod       2006-09-07 18:02:49.000000000 +0200
@@ -527,10 +527,39 @@
 =head2 Stringification
 
 Employ the standard stringification technique by using the C<overload>
-module.  Replace C<foo> with the column/method of your choice.
+module.
+
+To make an object stringify itself as a single column, use something like this 
(eplace C<foo> 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;
+
+B<Example>
+
+Suppose we have two tables: C<Product> and C<Category>. The table 
specifications are:
+
+  Product(id, Description, category)
+  Category(id, Description)
+
+C<category> 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 mapping the Category table (tipically C<DB::Main::Category>):
+
+  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
_______________________________________________
List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
Wiki: http://dbix-class.shadowcatsystems.co.uk/
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
Searchable Archive: http://www.mail-archive.com/[email protected]/

Reply via email to