Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 419
Jon Avatar asked Aug 09 '12 22:08

Jon


1 Answers

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');

like image 108
Alexander Hartmaier Avatar answered Oct 31 '22 17:10

Alexander Hartmaier