[Dbix-class] ResultSetManager and UPDATE + WHERE

Bill Moseley moseley at hank.org
Mon Apr 23 06:53:29 GMT 2007


Since mst has been questioning my sanity, I thought I'd verify this:

With CDBI I might have a FormToken class where I could get a new token
using this class method:

    my $token = FormToken->new_token;


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?


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?


Thanks,


-- 
Bill Moseley
moseley at hank.org




More information about the Dbix-class mailing list