[Dbix-class] ResultSetManager and UPDATE + WHERE
stephen joseph butler
stephen.butler at gmail.com
Mon Apr 23 07:15:57 GMT 2007
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 <moseley at hank.org> 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.
More information about the Dbix-class
mailing list