X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FManual%2FSQLHackers%2FUPDATE.pod;fp=lib%2FDBIx%2FClass%2FManual%2FSQLHackers%2FUPDATE.pod;h=1620acd31eb3561e1daad9622bf5fa170cefb64e;hb=07acc18d929eb449f0f83a9fde8bd34b13abf40f;hp=4e88e243a8195a5337cc3499e70c2e5f70e82b99;hpb=59f8b8d94c034c2166d8b98b3a99dd3271f40db6;p=dbsrgits%2FDBIx-Class-Manual-SQLHackers.git diff --git a/lib/DBIx/Class/Manual/SQLHackers/UPDATE.pod b/lib/DBIx/Class/Manual/SQLHackers/UPDATE.pod index 4e88e24..1620acd 100644 --- a/lib/DBIx/Class/Manual/SQLHackers/UPDATE.pod +++ b/lib/DBIx/Class/Manual/SQLHackers/UPDATE.pod @@ -139,71 +139,6 @@ To update a whole set of rows, or all of them, we first need to create a ResultS =back -=head2 Update a row or rows using a column calculation - - -- Yet another pointless example - UPDATE users - SET username = username || '.uk' - WHERE id = 1; - -=over - -=item 1. Create a Schema object representing the database you are working with: - - my $schema = MyDatabase::Schema->connect('dbi:SQLite:my.db'); - -=item 2. Call the B method on the resultset for the L you wish to fetch data from: - - my $fred_user = $schema->resultset('User')->find({ id => 1 }); - -The Row object has an B method that will change the values on -the object, and send an UPDATE query to the database. - -=item 3. Call the B method, passing it a hashref of new data: - -# this won't yet work, DBIC for now mandates the [ {} => $value ] format, the simple \[ $sql, $value1, $value2 ] will start being recognized later on -# the only documentation we currently have is this, if you can turn it into a DBIC pod-patch it will be freaking awesome -# https://github.com/dbsrgits/dbix-class/commit/0e773352 - $fred_user->update({ username => \['username || ?', '.uk'] }); - -# the DBIC syntax is a tad different from te thing above (i.e. we no longer encourage 'dummy' crap) -The \[ .. ] syntax here is described in L -documentation, used for passing bind parameters. - - -=back - -=head2 Update a row based on data in other tables - - -- Slightly less pointless example - UPDATE posts - SET title = user.username || title - JOIN users user ON user.id = posts.user_id; - -Joining two tables for an update is a similar sort of exercise to -joining them for a select query and using data from both. - -=over - -=item 1. Create a Schema object representing the database you are working with: - - my $schema = MyDatabase::Schema->connect('dbi:SQLite:my.db'); - -=item 2. Call the B method on the resultset for the L you wish to update data in, joining to the second table: - - my $posts = $schema->resultset('Post')->search( - {}, - { join => 'user' } - ); - - The B key takes as an argument a nested structure of one or more relation names (see L). - -=item 3. Call the B method on the resultset to run the UPDATE statement: - - $posts->update({ 'me.title' => \[ 'user.username || me.title' ] }); - -=back - =head2 Update or create a row -- MySQL non-standardness (and another silly example) @@ -221,8 +156,8 @@ joining them for a select query and using data from both. SET id = ?, username = ?, dob = ?, realname = ?, password = ?; COMMIT; -DBIx::Class does not produce the non-standard MySQL "ON DUPLICATE KEY -UPDATE", instead it has a shortcut for combining *find* and *update*. +DBIx::Class does not yet produce the non-standard MySQL "ON DUPLICATE KEY +UPDATE", instead it has a shortcut for combining B and B. To avoid race conditions, this should be done in a transaction.