perl催化剂关系has_many

时间:2015-02-12 15:53:39

标签: mysql perl catalyst dbix-class

工作代码是

sub category :Local {
    my ( $self, $c, $path ) = @_;

    my @cat_obj = $c->model('SQL::LasercalibrationCategory')->search({ category_id => $path } );
       my @nav; my $loop = 0; foreach my $r (@cat_obj) {$nav[$loop] = $r->id; $loop++;}
    my $result = $c->model("SQL::Lasercalibration")->search({ id => {-in => [@nav]}  }, { rows => 10 });

     my $page = $c->request->param('page') || 1;
     $page = 1 if ($page !~ /^\d+$/);
     my $rresult = $result->page($page);
     return $c->forward('default') if $rresult == 0;
     $c->stash->{items} = [ $rresult->all ];
     $c->stash->{page} = $page;
     $c->stash->{pager} = $rresult->pager;
     $c->stash->{template} = 'index.tt';
}

这是mysql上的数据库st,table是lasercalibration和lasercalibration_category。

lasercalibration_category包含表laserqibration中的所有id,row category_id包含类别关系。

例如在点击http://lasercalibration/category/2时在Catalyst上 它首先收集数组@cat_obj catagory关系。例如,类别/ 2关系是id 9735 18388 18946 19203 etc.,然后解析我$result = $c->model("SQL::Lasercalibration")->search({ id => {-in => [@nav]} }, { rows => 10 });

中的所有ID

我如何通过has_many或has_many_to_many dbxi关系来实现? 架构:

Lasercalibration.pm

package lasercalibration::Schema::Lasercalibration;

use strict;
use warnings;

use base 'DBIx::Class';

__PACKAGE__->load_components("Core");
__PACKAGE__->table("lasercalibration");
__PACKAGE__->add_columns(
  "id",
  { data_type => "INT", default_value => undef, is_nullable => 0, size => 10 },
  "price",
  { data_type => "FLOAT", default_value => undef, is_nullable => 0, size => 32 },
  "title",
  {
    data_type => "VARCHAR",
    default_value => undef,
    is_nullable => 0,
    size => 255,
  },
  "description",
  {
    data_type => "TEXT",
    default_value => undef,
    is_nullable => 0,
    size => 65535,
  },
  "autoinc",
  { data_type => "INT", default_value => undef, is_nullable => 0, size => 11 },
);
__PACKAGE__->set_primary_key("id");


# Created by DBIx::Class::Schema::Loader v0.04006 @ 2015-02-10 05:55:18
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:uyxHW0E8P7HwFd4a0Zf0lw


# You can replace this text with custom content, and it will be preserved on regeneration
1;

LasercalibrationCategory.pm

package lasercalibration::Schema::LasercalibrationCategory;

use strict;
use warnings;

use base 'DBIx::Class';

__PACKAGE__->load_components("Core");
__PACKAGE__->table("lasercalibration_category");
__PACKAGE__->add_columns(
  "id",
  { data_type => "INT", default_value => undef, is_nullable => 0, size => 10 },
  "category_id",
  { data_type => "INT", default_value => undef, is_nullable => 0, size => 11 },
);
__PACKAGE__->set_primary_key("id");


# Created by DBIx::Class::Schema::Loader v0.04006 @ 2015-02-10 05:55:18
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:QFvxaRhTcV7FX13vV4lq7w


# You can replace this text with custom content, and it will be preserved on regeneration
1;

0 个答案:

没有答案