How to get the value of a foreign key, not an object?

I have two tables, book and language ; book belongs_to language , having a language column that indicates what language it is in. The language table is just the language column.

I want to make $book->language and get the language string without selecting the language from the language table. Is there any way to do this?

I suspect this in the context of the return. Should I do some kind of overload, say:

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

But in this case, of course, I still get the language.

+6
source share
1 answer

One solution is to define relationships with different names than columns, for example. rel_$colname . Then the access methods created by DBIC will differ for the value of the column and the associated object (s).

If you do not want to change the relationship names, you can always access the column value using $row->get_column('colname');

+6
source

Source: https://habr.com/ru/post/922522/


All Articles