我试图了解如何使用数字调查:地图集。
如果我想有几组记录,使各团体本身成为各团体的成员,我就可能会形成一个图象,其中包括:
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 = ? )
如果有人想建议我如何误解许多人——到——我们的关系桥梁,并纠正我的许多——到——我们的职能定义,我将不胜感激。