X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FManual%2FFAQ.pod;h=9281a992c741a5e10b00c285b2688cc17b351860;hb=0a064375d784ed0ae14c345c5766db048abb8a2b;hp=df12b83214c73cc9533b1baf37146b1cd8d5a00b;hpb=a9ee81233ca46588fb723af4b36c88ae9aed5d2a;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Manual/FAQ.pod b/lib/DBIx/Class/Manual/FAQ.pod index df12b83..9281a99 100644 --- a/lib/DBIx/Class/Manual/FAQ.pod +++ b/lib/DBIx/Class/Manual/FAQ.pod @@ -56,6 +56,12 @@ Create your classes manually, as above. Write a script that calls L. See there for details, or the L. +=item .. store/retrieve Unicode data in my database? + +Make sure you database supports Unicode and set the connect +attributes appropriately - see +L + =item .. connect to my database? Once you have created all the appropriate table/source classes, and an @@ -126,7 +132,7 @@ allow you to supply a hashref containing the condition across which the tables are to be joined. The condition may contain as many fields as you like. See L. -=item .. define a relatiopnship across an intermediate table? (many-to-many) +=item .. define a relationship across an intermediate table? (many-to-many) Read the documentation on L. @@ -182,15 +188,9 @@ attribute. See L. =item .. sort my results based on fields I've aliased using C? -You don't. You'll need to supply the same functions/expressions to -C, as you did to C attribute, such as: - - ->search({}, { select => [ \'now() AS currenttime'] }) - -Then you can use the alias in your C attribute. +You didn't alias anything, since L +B with the produced SQL. See +L for details. =item .. group the results of my search? @@ -199,15 +199,7 @@ attribute, see L. =item .. group my results based on fields I've aliased using C? -You don't. You'll need to supply the same functions/expressions to -C, as you did to C attribute, such as: - - ->search({}, { select => [ \'now() AS currenttime'] }) - -Then you can use the alias in your C attribute. +You don't. See the explanation on ordering by an alias above. =item .. filter the results of my search? @@ -247,7 +239,13 @@ documentation for details. To use an SQL function on the left hand side of a comparison: - ->search({ -nest => \[ 'YEAR(date_of_birth)', [ dummy => 1979 ] ] }); + ->search({ -nest => \[ 'YEAR(date_of_birth) = ?', [ plain_value => 1979 ] ] }); + +Note: the C string in the C<< [ plain_value => 1979 ] >> part +should be either the same as the name of the column (do this if the type of the +return value of the function is the same as the type of the column) or +otherwise it's essentially a dummy string currently (use C as a +habit). It is used by L to handle special column types. Or, if you have quoting off: @@ -365,6 +363,9 @@ C supplied with C. =item .. insert many rows of data efficiently? +The C method in L provides +efficient bulk inserts. + =item .. update a collection of rows at the same time? Create a resultset using a search, to filter the rows of data you @@ -424,6 +425,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 @@ -511,6 +544,65 @@ You can reduce the overhead of object creation within L using the tips in L and L +=item How do I override a run time method (e.g. a relationship accessor)? + +If you need access to the original accessor, then you must "wrap around" the original method. +You can do that either with L or L. +The code example works for both modules: + + package Your::Schema::Group; + use Class::Method::Modifiers; + + # ... declare columns ... + + __PACKAGE__->has_many('group_servers', 'Your::Schema::GroupServer', 'group_id'); + __PACKAGE__->many_to_many('servers', 'group_servers', 'server'); + + # if the server group is a "super group", then return all servers + # otherwise return only servers that belongs to the given group + around 'servers' => sub { + my $orig = shift; + my $self = shift; + + return $self->$orig(@_) unless $self->is_super_group; + return $self->result_source->schema->resultset('Server')->all; + }; + +If you just want to override the original method, and don't care about the data +from the original accessor, then you have two options. Either use +L that does most of the work for you, or do +it the "dirty way". + +L way: + + package Your::Schema::Group; + use Method::Signatures::Simple; + + # ... declare columns ... + + __PACKAGE__->has_many('group_servers', 'Your::Schema::GroupServer', 'group_id'); + __PACKAGE__->many_to_many('servers', 'group_servers', 'server'); + + # The method keyword automatically injects the annoying my $self = shift; for you. + method servers { + return $self->result_source->schema->resultset('Server')->search({ ... }); + } + +The dirty way: + + package Your::Schema::Group; + use Sub::Name; + + # ... declare columns ... + + __PACKAGE__->has_many('group_servers', 'Your::Schema::GroupServer', 'group_id'); + __PACKAGE__->many_to_many('servers', 'group_servers', 'server'); + + *servers = subname servers => sub { + my $self = shift; + return $self->result_source->schema->resultset('Server')->search({ ... }); + }; + =back =head2 Notes for CDBI users @@ -541,7 +633,7 @@ Likely you have/had two copies of postgresql installed simultaneously, the second one will use a default port of 5433, while L is compiled with a default port of 5432. -You can chance the port setting in C. +You can change the port setting in C. =item I've lost or forgotten my mysql password