1

次のスキーマがあります。

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;
}

「数」時間のデバッグの後、私はそれを修正することができました。

時間をかけて読んで、なぜこれがうまくいかなかったのかを理解しようとしてくれたすべての人に感謝します.

4

0 に答える 0