How do I get the value of a foreign key rather than the object?
Asked Answered
T

1

6

I've got two tables, book and language; book belongs_to language by having a language column stating which language it's in. The language table is just the language column.

I want to do $book->language and get the language string, without fetching the language from the language table. Is there a way to do that?

I suspect it's about return context. Should I do some sort of overload, say:

use overload "language_string" => sub {
  my $self = shift;
  return $self->language;
}, fallback => 1;

But in that case I'm, of course, still getting the language.

Technology answered 9/8, 2012 at 22:42 Comment(0)
Z
6

One solution is to define the relationships with different names than the columns e.g. rel_$colname. Then the accessor methods generated by DBIC will be different for the column value and the related object(s).

If you don't want to change your relationship names you can always access the column value with $row->get_column('colname');

Zimmer answered 10/8, 2012 at 10:41 Comment(1)
Thanks, get_column was exactly what I was hunting for!Technology

© 2022 - 2024 — McMap. All rights reserved.