X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FManual%2FFAQ.pod;h=df0f77373771bec23c167961c4521423548135d0;hb=ca83d811f3ce60ebb819ce9ad54179837f6157c6;hp=928808cb4a37dd61e54458d8f46ee60b45d4e0f9;hpb=36d18cd952538e9217aaf2cfd2568a9409d46e92;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/lib/DBIx/Class/Manual/FAQ.pod b/lib/DBIx/Class/Manual/FAQ.pod index 928808c..df0f773 100644 --- a/lib/DBIx/Class/Manual/FAQ.pod +++ b/lib/DBIx/Class/Manual/FAQ.pod @@ -255,6 +255,36 @@ Call C on a L, this returns a L, see it's documentation and the L for details. +=item .. fetch a formatted column? + +In your table schema class, create a "private" column accessor with: + + __PACKAGE__->add_columns(my_common => { accessor => '_hidden_my_column' }); + +Then, in the same class, implement a subroutine called "my_column" that +fetches the real value and does the formatting you want. + +See the Cookbook for more details. + +=item .. fetch a single (or topmost) row? + +Sometimes you many only want a single record back from a search. A quick +way to get that single row is to first run your search as usual: + + ->search->(undef, { order_by => "id DESC" }) + +Then call L and ask it only to return 1 row: + + ->slice(0,1) + +These two calls can be combined into a single statement: + + ->search->(undef, { order_by => "id DESC" })->slice(0,1) + +Why slice instead of L or L? +If supported by the database, slice will use LIMIT/OFFSET to hint to the database that we +really only need one row. This can result in a significant speed improvement. + =back =head2 Inserting and updating data @@ -296,6 +326,35 @@ scalar reference: ->update({ somecolumn => \'othercolumn' }) +=item .. store JSON/YAML in a column and have it deflate/inflate automatically? + +You can use L to accomplish YAML/JSON storage transparently. + +If you want to use JSON, then in your table schema class, do the following: + + use JSON; + + __PACKAGE__->add_columns(qw/ ... my_column ../) + __PACKAGE__->inflate_column('my_column', { + inflate => sub { jsonToObj(shift) }, + deflate => sub { objToJson(shift) }, + }); + +For YAML, in your table schema class, do the following: + + use YAML; + + __PACKAGE__->add_columns(qw/ ... my_column ../) + __PACKAGE__->inflate_column('my_column', { + inflate => sub { YAML::Load(shift) }, + deflate => sub { YAML::Dump(shift) }, + }); + +This technique is an easy way to store supplemental unstructured data in a table. Be +careful not to overuse this capability, however. If you find yourself depending more +and more on some data within the inflated column, then it may be time to factor that +data out. + =back =head2 Misc