X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FManual%2FFAQ.pod;h=8a706e1c78387e2581f5eb8c1a0e15e364489dd0;hb=9361b05d319e60314bf2caff1e96ff3c388a50bb;hp=bbdfcf59bc74f64b5bfe5130bc76391e4b7291ee;hpb=c0e1e94971b9ba42a055da8c22d75831f527f53e;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Manual/FAQ.pod b/lib/DBIx/Class/Manual/FAQ.pod index bbdfcf5..8a706e1 100644 --- a/lib/DBIx/Class/Manual/FAQ.pod +++ b/lib/DBIx/Class/Manual/FAQ.pod @@ -20,7 +20,7 @@ How Do I: =item .. create a database to use? -First, choose a database. For testing/experimenting, we reccommend +First, choose a database. For testing/experimenting, we recommend L, which is a self-contained small database (i.e. all you need to do is to install L from CPAN, and it works). @@ -132,9 +132,10 @@ 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 relationship across an intermediate table? (many-to-many) +=item .. define a relationship bridge across an intermediate table? (many-to-many) -Read the documentation on L. +The term 'relationship' is used loosely with many_to_many as it is not considered a +relationship in the fullest sense. For more info, read the documentation on L. =item .. stop DBIx::Class from attempting to cascade deletes on my has_many and might_have relationships? @@ -238,19 +239,18 @@ documentation for details. =item .. search with an SQL function on the left hand side? -To use an SQL function on the left hand side of a comparison: +To use an SQL function on the left hand side of a comparison you currently need +to resort to literal SQL: - ->search({ -nest => \[ 'YEAR(date_of_birth) = ?', [ plain_value => 1979 ] ] }); + ->search( \[ '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: - - ->search({ 'YEAR(date_of_birth)' => 1979 }); +return value of the function is the same as the type of the column) or in the +case of a function it's currently treated as a dummy string (it is a good idea +to use C or something similar to convey intent). The value is +currently only significant when handling special column types (BLOBs, arrays, +etc.), but this may change in the future. =item .. find more help on constructing searches? @@ -304,7 +304,7 @@ Use the L and L attributes to order your data and pick off a single row. -See also L. +See also L. A less readable way is to ask a regular search to return 1 row, using L: @@ -381,10 +381,11 @@ the rows at once. =item .. update a column using data from another column? -To stop the column name from being quoted, you'll need to supply a -scalar reference: +To stop the column name from being quoted, you'll need to tell DBIC +that the right hand side is an SQL identifier (it will be quoted +properly if you have quoting enabled): - ->update({ somecolumn => \'othercolumn' }) + ->update({ somecolumn => { -ident => 'othercolumn' } }) This method will not retrieve the new value and put it in your Row object. To fetch the new value, use the C method on @@ -401,7 +402,7 @@ the Row. To update and refresh at once, chain your calls: - $row->update({ 'somecolumn' => \'othercolumn' })->discard_changes; + $row->update({ 'somecolumn' => { -ident => 'othercolumn' } })->discard_changes; =item .. store JSON/YAML in a column and have it deflate/inflate automatically? @@ -436,8 +437,8 @@ data out. =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 +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 { @@ -454,7 +455,7 @@ 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 +what create_related() from L does, you could add this to Book.pm: sub foo { @@ -472,25 +473,25 @@ Invoked like this: =item How do I store my own (non-db) data in my DBIx::Class objects? -You can add your own data accessors to your classes. +You can add your own data accessors to your Result classes. One method is to use the built in mk_group_accessors (via L) - package MyTable; + package App::Schema::Result::MyTable; - use parent 'DBIx::Class'; + use parent 'DBIx::Class::Core'; __PACKAGE__->table('foo'); #etc __PACKAGE__->mk_group_accessors('simple' => qw/non_column_data/); # must use simple group An another method is to use L with your L package. - package MyTable; + package App::Schema::Result::MyTable; use Moose; # import Moose use Moose::Util::TypeConstraint; # import Moose accessor type constraints - extends 'DBIx::Class'; # Moose changes the way we define our parent (base) package + extends 'DBIx::Class::Core'; # Moose changes the way we define our parent (base) package has 'non_column_data' => ( is => 'rw', isa => 'Str' ); # define a simple attribute @@ -566,12 +567,12 @@ 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 { @@ -591,12 +592,12 @@ 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({ ... }); @@ -606,17 +607,17 @@ 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