X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FManual%2FFAQ.pod;h=a3fe023972d9c7179f9d0c01bb948f8da3b4f3c5;hb=7cf4ae7a66b75a490eec84ce154bf306e1ff8744;hp=d37211521fa292e4b6befca101872f5586260d47;hpb=aefa6508d3b034fb36d1a919c5be0aa363c52328;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Manual/FAQ.pod b/lib/DBIx/Class/Manual/FAQ.pod index d372115..a3fe023 100644 --- a/lib/DBIx/Class/Manual/FAQ.pod +++ b/lib/DBIx/Class/Manual/FAQ.pod @@ -77,7 +77,7 @@ lot later. =item .. tell DBIx::Class about relationships between my tables? -There are a vareity of relationship types that come pre-defined for +There are a variety of relationship types that come pre-defined for you to use. These are all listed in L. If you need a non-standard type, or more information, look in L. @@ -113,12 +113,19 @@ as you like. See L. Read the documentation on L. -=item .. stop DBIx::Class from attempting to cascade deletes on my has_many relationships? +=item .. stop DBIx::Class from attempting to cascade deletes on my has_many and might_have relationships? By default, DBIx::Class cascades deletes and updates across -C relationships. If your database already does this (and -that is probably better), turn it off by supplying C<< cascade_delete => 0 >> -in the relationship attributes. See L. +C and C relationships. You can disable this +behaviour on a per-relationship basis by supplying +C<< cascade_delete => 0 >> in the relationship attributes. + +The cascaded operations are performed after the requested delete or +update, so if your database has a constraint on the relationship, it +will have deleted/updated the related records or raised an exception +before DBIx::Class gets to perform the cascaded operation. + +See L. =item .. use a relationship? @@ -232,6 +239,25 @@ and not: my $interval = "now() - interval '12 hours'"; ->search({last_attempt => { '<' => \$interval } }) +=item .. search with an SQL function on the left hand side? + +To use an SQL function on the left hand side of a comparison: + + ->search({}, { where => \'YEAR(date_of_birth)=1979' }); + +=begin hidden + +(When the bind arg ordering bug is fixed, the previous example can be +replaced with the following.) + + ->search({}, { where => \'YEAR(date_of_birth)=?', bind => [ 1979 ] }); + +=end hidden + +Or, if you have quoting off: + + ->search({ 'YEAR(date_of_birth' => 1979 }); + =item .. find more help on constructing searches? Behind the scenes, DBIx::Class uses L to help construct @@ -239,6 +265,18 @@ its SQL searches. So if you fail to find help in the L, try looking in the SQL::Abstract documentation. +=item .. make searches in Oracle (10gR2 and newer) case-insensitive? + +To make Oracle behave like most RDBMS use on_connect_do to issue +alter session statements on database connection establishment: + + ->on_connect_do("ALTER SESSION SET NLS_COMP = 'LINGUISTIC'"); + ->on_connect_do("ALTER SESSION SET NLS_SORT = '_CI'"); + e.g. + ->on_connect_do("ALTER SESSION SET NLS_SORT = 'BINARY_CI'"); + ->on_connect_do("ALTER SESSION SET NLS_SORT = 'GERMAN_CI'"); + + =back =head2 Fetching data @@ -275,11 +313,11 @@ way to get that single row is to first run your search as usual: Then call L and ask it only to return 1 row: - ->slice(0,1) + ->slice(0) These two calls can be combined into a single statement: - ->search->(undef, { order_by => "id DESC" })->slice(0,1) + ->search->(undef, { order_by => "id DESC" })->slice(0) Why slice instead of L or L? If supported by the database, slice will use LIMIT/OFFSET to hint to the database that we @@ -319,7 +357,7 @@ primary key field from the sequence. To help PK::Auto find your inserted key, you can tell it the name of the sequence in the C supplied with C. - ->add_columns({ id => { sequence => 'mysequence' } }); + ->add_columns({ id => { sequence => 'mysequence', auto_nextval => 1 } }); =item .. insert many rows of data efficiently? @@ -338,6 +376,19 @@ scalar reference: ->update({ somecolumn => \'othercolumn' }) +But note that when using a scalar reference the column in the database +will be updated but when you read the value from the object with e.g. + + ->somecolumn() + +you still get back the scalar reference to the string, B the new +value in the database. To get that you must refresh the row from storage +using C. Or chain your function calls like this: + + ->update->discard_changes + + to update the database and refresh the object in one step. + =item .. store JSON/YAML in a column and have it deflate/inflate automatically? You can use L to accomplish YAML/JSON storage transparently. @@ -400,6 +451,17 @@ is executed. You can create further resultset refinements by calling search again or relationship accessors. The SQL query is only run when you ask the resultset for an actual row object. +=item How do I deal with tables that lack a primary key? + +If your table lacks a primary key, DBIx::Class can't work out which row +it should operate on, for example to delete or update. However, a +UNIQUE constraint on one or more columns allows DBIx::Class to uniquely +identify the row, so you can tell L these +columns act as a primary key, even if they don't from the database's +point of view: + + $resultset->set_primary_key(@column); + =back =head2 Notes for CDBI users