English 中文(简体)
• 自愿加入联邦调查局的团体: 班级
原标题:groups of groups using self joins in DBIx::Class

我试图了解如何使用数字调查:地图集。

如果我想有几组记录,使各团体本身成为各团体的成员,我就可能会形成一个图象,其中包括:

CREATE TABLE groups (
       id    INTEGER PRIMARY KEY,
       name  VARCHAR(100)
       );

CREATE TABLE group_groups (
       parent_id         INTEGER REFERENCES groups(id),
       child_id          INTEGER REFERENCES groups(id),
       PRIMARY KEY(parent_id,child_id)
       );

如果我使用DBIx:Class:Schema:Loader 丢掉这个表象,我有以下关系:

Group.pm

__PACKAGE__->has_many(
  "group_groups_children",
  "Schema::Result::GroupGroup",
  { "foreign.child_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);

__PACKAGE__->has_many(
  "group_groups_parents",
  "Schema::Result::GroupGroup",
  { "foreign.parent_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);

GroupGroup.pm

__PACKAGE__->belongs_to(
  "child",
  "Schema::Result::Group",
  { id => "child_id" },
  {
    is_deferrable => 1,
    join_type     => "LEFT",
    on_delete     => "CASCADE",
    on_update     => "CASCADE",
  },
);

__PACKAGE__->belongs_to(
  "parent",
  "Schema::Result::Group",
  { id => "parent_id" },
  {
    is_deferrable => 1,
    join_type     => "LEFT",
    on_delete     => "CASCADE",
    on_update     => "CASCADE",
  },
);

我认为,在小组桌旁的两边,我需要一间“人际关系”桥梁,因此我创建了:

__PACKAGE__->many_to_many(
     childgroups  =>  group_groups_children  ,  child );

__PACKAGE__->many_to_many(
     parents  =>  group_groups_parents  ,  parent );

由于我对“关系桥梁”定义的理解类似:从来者_名称=和;在相关表格中,“归属”名称与“关系”之间的关系。

当我尝试这一法典时:

my $group_rs = $schema->resultset( Group )->search(
        { id => $id }
);
my $group = $group_rs->first;
foreach my $child ($group->childgroups) {
<snip>

3. 设立银行——TRACE 显示:

SELECT child.id, child.name FROM group_groups me 
       JOIN groups child ON child.id = me.child_id  
       WHERE ( me.child_id = ? )

但我认为,这一思路应当更像:

SELECT child.id, child.name FROM group_groups me 
       JOIN groups child ON child.id = me.child_id 
       JOIN groups parent ON parent.parent_id = me.id 
       WHERE ( me.child_id = ? )

如果有人想建议我如何误解许多人——到——我们的关系桥梁,并纠正我的许多——到——我们的职能定义,我将不胜感激。

最佳回答

当有人要求,Sribasushi帮助我解决这一问题。 很显然,美国银行协会:Schema:Loader制造不正确,在G.pm中产生不正确的关系,它应当这样看待:

__PACKAGE__->has_many(
  "group_groups_parents",
  "Schema::Result::GroupGroup",
  { "foreign.child_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);

__PACKAGE__->has_many(
  "group_groups_children",
  "Schema::Result::GroupGroup",
  { "foreign.parent_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);

原件:

has_many (group_groups_children => "Schema::Result::GroupGroup", { "foreign.child_id" => self.id" } )

也就是说,“我亲爱的孩子的所有牢房”,实际上,这给我亲爱的孩子的父母,而不是孩子。

我的表象是好的,我的关系定义是好的,我的用法是好的,汽车产生的关系并不足以发现失败。

ribasushi hinted thatpoint restrictions (i.e "better” DDL for my schema may have contributed DBICSL now it right.

问题回答

暂无回答




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签