[Dbix-class] DBIx::Class column list
Bruce J Keeler
bruce at drangle.com
Mon Mar 10 17:39:59 GMT 2008
Ryan D Johnson wrote:
> xyon <xyon at indigorobot.com> writes:
>
>
>> I just want to fetch the column names. Below is essentially what I
>> want to do (but it obviously doesn't work;)):
>>
>> my @columns = $schema->resultset('data')->columns;
>>
>
>
I also have this need.
> Is this what you're looking for:
>
> $ $schema->resultset('User')->result_source->columns;
> $ARRAY1 = [
> 'id',
> 'email',
> 'username',
> 'password',
> 'signup_time',
> 'activation_code',
> 'invitations_left'
> ];
>
And this isn't what I need. A result set might have columns from joined
tables, or it might not have all of the columns from its base table.
I have the following in my custom ResultSet class, which does what I
need. Perhaps it would be a useful addition to the base ResultSet?
sub columns {
my $self = shift;
my $attr = $self->_resolved_attrs;
return $attr->{as};
}
sub column_info {
my $self = shift;
my $col = shift;
my $attr = $self->_resolved_attrs;
return $attr->{column_info}{$col}
if exists $attr->{column_info} && $attr->{column_info}{$col};
my $source = $self->result_source;
while (my ($left, $right) = $col =~ /^([^.]*)\.(.*)$/) {
$source = $source->related_source($left);
$col = $right;
}
return $source->column_info($col);
}
More information about the DBIx-Class
mailing list