Perl DBIx::类查找或创建错误-无法以类方法获取数据

Perl DBIx::类查找或创建错误-无法以类方法获取数据,perl,dbix-class,Perl,Dbix Class,我有以下模式: package Food::Schema::Result::Order; # Created by DBIx::Class::Schema::Loader # DO NOT MODIFY THE FIRST PART OF THIS FILE use strict; use warnings; use base 'DBIx::Class::Core'; =head1 NAME Food::Schema::Result::Order =cut __PACKAGE__

我有以下模式:

package Food::Schema::Result::Order;

# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE

use strict;
use warnings;

use base 'DBIx::Class::Core';


=head1 NAME

Food::Schema::Result::Order

=cut

__PACKAGE__->table("orders");

=head1 ACCESSORS

=head2 id

  data_type: 'integer'
  is_auto_increment: 1
  is_nullable: 0

=head2 company_id

  data_type: 'integer'
  is_nullable: 1

=head2 user_id

  data_type: 'integer'
  is_nullable: 1

=head2 total

  data_type: 'float'
  is_nullable: 1

=head2 money_id

  data_type: 'integer'
  is_nullable: 1

=head2 created_at

  data_type: 'datetime'
  datetime_undef_if_invalid: 1
  is_nullable: 1

=head2 updated_at

  data_type: 'bigint'
  is_nullable: 1

=head2 status

  data_type: 'varchar'
  is_nullable: 1
  size: 10

=cut

__PACKAGE__->add_columns(
  "id",
  { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
  "company_id",
  { data_type => "integer", is_nullable => 1 },
  "user_id",
  { data_type => "integer", is_nullable => 1 },
  "total",
  { data_type => "float", is_nullable => 1 },
  "money_id",
  { data_type => "integer", is_nullable => 1 },
  "created_at",
  {
    data_type => "datetime",
    datetime_undef_if_invalid => 1,
    is_nullable => 1,
  },
  "updated_at",
  { data_type => "bigint", is_nullable => 1 },
  "status",
  { data_type => "varchar", is_nullable => 1, size => 10 },
);
__PACKAGE__->set_primary_key("id");


# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-12-29 12:31:26
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:TZMuN6qiqXlDLR361KLqWg
use DateTime;
__PACKAGE__->remove_column('created_at');
__PACKAGE__->add_columns(
  "created_at",
  {
    data_type => "datetime",
    datetime_undef_if_invalid => 1,
    is_nullable => 0,
    accessor => '_created_at',
  },
);



__PACKAGE__->belongs_to(company => 'Food::Schema::Result::Company', 'company_id');
__PACKAGE__->belongs_to(user => 'Food::Schema::Result::User', 'user_id');
__PACKAGE__->has_many(order_lines => 'Food::Schema::Result::OrderLine', 'order_id');
__PACKAGE__->many_to_many(foods => 'order_lines', 'food');
__PACKAGE__->many_to_many(menus => 'order_lines', 'menu');

sub created_at{
 my ($self, $value) = @_;

 $self->_created_at( $self->_created_at() || DateTime::Format::MySQL->format_datetime( DateTime->now() ) ); 

 return $self->_created_at();
};

sub new{
 my ($self, $attrs) = @_;

 $self->created_at();

 my $new = $self->next::method($attrs);
 return $new;
}

1;
我尝试查找或创建基于某些属性的订单,如:

   my $order = $self->app->model->resultset('Order')->find_or_create({
                company_id => $self->param('company_id'),
                status     => 'open',
                user_id    => $self->app->sessions->{user}->id(),
   });
我得到了错误DBIx::Class::Row::get_column:无法将数据作为类方法获取

现在,如果我从find_或create中排除status列,它会按预期工作,但不会按需要工作-这就是为什么我想在子句中添加更多列的原因

如果在find_或create中添加任何其他列,则会出现该错误

有人知道这是一个bug还是预期的行为吗?我如何找到或创建基于更多列值的订单

编辑:

看起来这不是一个bug——我没有以正确的方式重写新方法

新方法必须如下所示:

sub new{
 my ($self, $attrs) = @_;

 $attrs->{created_at} = DateTime::Format::MySQL->format_datetime( DateTime->now() );

 my $new = $self->next::method($attrs);

 return $new;
}
经过“几个”小时的调试,我成功地解决了这个问题


感谢大家花时间阅读并试图找出这不起作用的原因。

我认为find_或create需要能够检查行是否已经存在,因此需要告诉DBIx::Class哪些字段子集具有唯一约束,即可以用于标识行。据我所知,您只有主键?是的,只有主键作为唯一约束。在这种情况下,find_或_create没有“id”的值是没有意义的,是吗?如果没有唯一的键值,它将无法找到任何内容。你还不如打电话给create?我知道这可能解决不了你的问题,我只是说。你能提供一个完整的stacktrace,用于“不能获取数据为类”方法错误吗?如果默认情况下没有获得,请安装并添加use-Carp::Always-Anywhere,然后再次运行。