我正在努力了解 DBiX,当我有多对多关系时,我正在努力解决如何将数据插入到我的数据库中。
在下面的示例中,对象和网络之间存在多对多关系。
一个对象有一个名称和描述一个网络有一个 IP 和一个网络掩码
我有一个多对多关系的连接表。
如何添加对象及其关联的网络?
例如我有
object
—————
Name : TestObject1
Description: TestObject1
使用以下网络
IP:1.1.1.1
Netmask:255.255.255.255
IP:2.2.2.2
Netmask:255.255.255.255
和
Name : TestObject2
Description: TestObject2
与以下网络
IP:1.1.1.1
Netmask:255.255.255.255
IP:3.3.3.3
Netmask:255.255.255.255
我知道如何插入具有一对多关系的数据,但我不知道如何处理多对多。理想情况下,我希望能够建立我的数据结构,然后一次性将其全部添加。
下面是由模式加载器创建的相关表。
谢谢。
Object Table
------------
__PACKAGE__->table("Object");
__PACKAGE__->add_columns(
"objectid",
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
"description",
{ data_type => "varchar", is_nullable => 1, size => 100 },
"objectname",
{ data_type => "varchar", is_nullable => 0, size => 100 },
);
__PACKAGE__->set_primary_key("objectid");
__PACKAGE__->has_many(
"object_has_networks",
"TestApp::Schema::Result::ObjectHasNetwork",
{ "foreign.object_objectid" => "self.objectid" },
{ cascade_copy => 0, cascade_delete => 0 },
);
__PACKAGE__->many_to_many(
"network_networkids",
"object_has_networks",
"network_networkid",
);
Network Table
------------
__PACKAGE__->table("network");
__PACKAGE__->add_columns(
"networkid",
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
“IP”,
{ data_type => "varchar", is_nullable => 1, size => 15 },
"netmask",
{ data_type => "varchar", is_nullable => 1, size => 15 },
);
__PACKAGE__->set_primary_key("networkid");
__PACKAGE__->has_many(
"object_has_networks",
"TestApp::Schema::Result::ObjectHasNetwork",
{ "foreign.network_networkid" => "self.networkid" },
{ cascade_copy => 0, cascade_delete => 0 },
);
__PACKAGE__->many_to_many("object_objectids", "object_has_networks", "object_objectid");
Junction Table
--------------
__PACKAGE__->table("Object_has_network");
__PACKAGE__->add_columns(
"object_objectid",
{ data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
"network_networkid",
{ data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
);
__PACKAGE__->set_primary_key("object_objectid", "network_networkid");:wq
__PACKAGE__->belongs_to(
"network_networkid",
"TestApp::Schema::Result::Network",
{ networkid => "network_networkid" },
{ is_deferrable => 1, on_delete => "NO ACTION", on_update => "NO ACTION" },
);
__PACKAGE__->belongs_to(
"object_objectid",
"TestApp::Schema::Result::Object",
{ objectid => "object_objectid" },
{ is_deferrable => 1, on_delete => "NO ACTION", on_update => "NO ACTION" },
);