简体   繁体   中英

Create DBIx ResultSet using column aliases

I have started to use DBIx::Class and I really like it for the most part but what is really starting to annoy me is the column alias does not seem to work fully.

Eg. Suppose I have this table definition:

#TestClass.pm

use strict;
use warnings;

package Database::Schema::Result::TestClass;

use base qw/DBIx::Class::Core/;

__PACKAGE__->table("TEST_TABLE");
__PACKAGE__->add_column("ID")
__PACKAGE__->add_columns(NAME => {accessor => "name"},
                         VALUE => {accessor => "value"}
                         );

And then I try to create a new row as follows:

 $schema->resultset("TestClass")->create(name => "test", value => "value");

The above will say: DBIx::Class::ResultSet::create(): No such column name on Database::Schema::Result::TestClass

However the following works fine:

 $schema->resultset("TestClass")->create(NAME => "test", VALUE => "value");

If later on I have TestClass object and try to access its columns as such:

 $object->NAME;

I get Can't locate object method "NAME" via package "Database::Schema::Result::TestClass"

but this is ok:

 $object->name

I would expect to be able to create the object using the accessor I provided the column and for creation of the object and accessing the columns to be consistent but this does not seem to be the case. Can anyone explain why this is?

I think the answer to your dilemma lies in part 3 of the DBIx::Class Tutorial . Also, the manual page for the add_columns method of DBIx::Class::ResultSource class states that: Use this (the accessor property) to set the name of the accessor method for this column. If unset, the name of the column will be used.

Basically what you did was to define a column NAME and create an accessor for it name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM