I've only been playing with DBIC for a couple weeks, so someone might
want to verify my answers...

On 4/23/07, Bill Moseley <[EMAIL PROTECTED]> wrote:
With DBIC I'm using ResultSetManager.  In my ResultSource / table
class I have:

    sub new_token : ResultSet {
        my $rs = shift;

        return $rs->create( { id => Data::UUID->new->create_str } );
    }

And then I call $schema->resultset('FormToken')->new_token;

Is that the correct and recommend approach?

Sure. It works, right?

Second, I thought I saw in the docs how to do update with a where
clause.   So, another example with the FormToken class.  Say I have a
$token and I want to invalidate it, but only if it's currently valid.

sub invalidate {
    my $token = shift;

    my $dbh = $token->result_source->schema->storage->dbh;

    my $sth = $dbh->prepare_cached( << '' );
        UPDATE form_token SET valid = FALSE WHERE id = ? AND valid = TRUE

    $sth->execute( $token->id );
    return $sth->rows;
}

What's the DBIC way to do that?

I think you want:

return $schema->resultset( 'FormToken' )->search( { id => $token->id,
valid => 'TRUE' } )->update( { valid => 'FALSE' } );

Key is to remember that search() doesn't do anything until it has to.

_______________________________________________
List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
Wiki: http://dbix-class.shadowcatsystems.co.uk/
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
Searchable Archive: http://www.mail-archive.com/[email protected]/

Reply via email to