Search code examples
perldbix-class

How can I alter a DBIx::Class object value without updating the actual DB


I am checking for mismatch usernames in one of my applications. If it finds a mismatch username, I wrap the value in <mark> tags.

I want to update my DBIx::Class object's username value before passing it back to my JS.

I currently have the below, which is almost what I want but not quite.

my $mismatch_username = check_for_mismatch_username($self, $id, 'AD');
    if(defined $mismatch_username)
    {
      my $flagged_username = "<mark>".$info->accountname."</mark> <font color=\"red\">(Mismatch: $mismatch_username)</font>";
       $info->update({accountname => $flagged_username});
    }
$info = {$info->get_columns};
$self->render(json => {info => $info});

It is very important I don't update the DB value. I only want to update the variable's value. I am currently still looking through the DBIx::Class documentation.

Any direction is appreciated.


Solution

  • Don't modify the object if you don't want it to be modified.

    (You could, of course, update the field without saving it using $info->accountname(…). But that would still leave the object in a modified state, where other code could potentially save it. It'd also potentially fail if the HTML markup was rejected by a validator on the field.)

    If all you actually need is to change the username in the JSON, generate the data that'll go into the JSON first, then update the appropriate field in the data before rendering it:

    my $data = { $info->get_columns };
    if (defined $mismatch_username) {
        $data->{accountname} = "<mark>…";
    }
    $self->render(json => { info => $data });