我有以下架构:
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;
我尝试find_or_create
根据一些属性下订单,例如:
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(): Can't fetch data as class method
现在,如果我从 中排除该status
列find_or_create
,它会按预期工作(但不是按需要 - 这就是我想在子句中添加更多列的原因)。
如果我在 中添加任何其他列find_or_create
,我会收到该错误。
有谁知道这是一个错误还是预期的行为,我如何find_or_create
根据更多的列值订购?
编辑:
看起来这不是一个错误 - 我没有new
以正确的方式覆盖该方法。
该new
方法必须是这样的:
sub new{
my ($self, $attrs) = @_;
$attrs->{created_at} = DateTime::Format::MySQL->format_datetime( DateTime->now() );
my $new = $self->next::method($attrs);
return $new;
}
经过“几个”小时的调试后,我设法解决了这个问题。
感谢所有花时间阅读并试图弄清楚为什么这不起作用的人。