简体   繁体   English

使用列别名创建DBIx ResultSet

[英]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. 我已经开始使用DBIx :: Class,并且在大多数情况下我真的很喜欢它,但是真正让我烦恼的是列别名似乎并没有完全起作用。

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 上面会说:DBIx :: Class :: ResultSet :: create():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: 如果以后有TestClass对象,请尝试按以下方式访问其列:

 $object->NAME;

I get Can't locate object method "NAME" via package "Database::Schema::Result::TestClass" 我通过包“ Database :: Schema :: Result :: TestClass”找不到对象方法“ NAME”

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 . 我认为您的困境的答案在于DBIx :: Class Tutorial的第3部分 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. 另外, DBIx::Class::ResultSource类的add_columns方法手册页指出: 使用此accessor属性) 设置此列的accessor方法的名称。 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 基本上你所做的是定义列NAME ,并为它创建访问name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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