FAQ
I have a database table -- let's call it 'Contacts' that I'm accessing
through a DBIx::Class model. This table has two fields called
'first_name' and 'last_name'.

Could I get some suggestion on the best way to add code to the model (or
the schema) to allow me to add a custom field to the resultset without
having an actual column in the database table? I want to be able to
access a field in the resultset called something like 'common_name'.
This field would contain a concatenation of the first and last name
fields for people and only the data from the last name for other
entities like businesses.

Thanks!

Greg Coates

Search Discussions

  • Matija Grabnar at Jan 21, 2009 at 6:41 pm

    Greg Coates wrote:
    I have a database table -- let's call it 'Contacts' that I'm accessing
    through a DBIx::Class model. This table has two fields called
    'first_name' and 'last_name'.

    Could I get some suggestion on the best way to add code to the model
    (or the schema) to allow me to add a custom field to the resultset
    without having an actual column in the database table? I want to be
    able to access a field in the resultset called something like
    'common_name'. This field would contain a concatenation of the first
    and last name fields for people and only the data from the last name
    for other entities like businesses.
    At the end of the schema definition file (if you're generating schema,
    it's after the DO NOT MODIFY THIS OR ANYTHING ABOVE! line)
    add the following:


    __PACKAGE__->resultset_class('Contacts::ResultSet');

    package Contacts::ResultSet;
    use warnings;
    use strict;
    use base 'DBIx::Class::ResultSet';

    sub common_name {
    if ($self->person) {
    return $self->first_name." ".$self->last_name;
    } else {
    # .... etc.....
    }
  • Tobias Kremer at Jan 21, 2009 at 6:49 pm

    On 21.01.2009, at 19:24, Greg Coates wrote:

    Could I get some suggestion on the best way to add code to the model
    (or the schema) to allow me to add a custom field to the resultset
    without having an actual column in the database table? I want to
    Just add them as methods to your ResultSource classes:

    package MyApp::Schema::Contacts;

    [...]

    sub common_name {
    my $self = shift;
    return $self->first_name . " " . $self->last_name;
    }

    --Tobias

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedJan 21, '09 at 6:24p
activeJan 21, '09 at 6:49p
posts3
users3
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2021 Grokbase