[Dbix-class] Updating lookup ID based on corresponding string

Tim King timk at theperlshop.com
Thu Sep 17 21:20:15 GMT 2015


Hi, all.

I have lookup-table relationships of the following form:

__PACKAGE__->belongs_to(
    size => 'Schema::Result::SizeLookup',
    { 'foreign.size_id' => 'self.size_id' },
);


Now, I know that I can create a new row like this (all made-up
pseudocode, so please forgive any typos), and have DBIC look up the
size_id from the SizeLookup table:

$schema->resultset('TeeShirt')->create({
    serial_number => 'XYZ1234',
    size => { name => 'large' },
});


But I would really like to be able to do something *similar* to this:

$schema->resultset('TeeShirt')->create({
    serial_number => 'XYZ1234',
    size_name => 'large',
});

$tee_shirt->update({
    size_name => 'small',
});


We're a little surprised that this isn't a common desire, to be able to
create/update looked-up IDs based on the corresponding identifying
string. But we can't seem to find any off-the-shelf component that will
enable something like this.

We did see DBIx::Class::LookupColumn, but that provides additional
methods, not column-like functionality.

Any suggestions?

Tim

-- 
Tim King
Lead Developer, The Perl Shop
http://www.theperlshop.com/




More information about the DBIx-Class mailing list