thi*_*ger 4 database perl dbix-class
使用以下(简化)MySQL表定义:
create table items (
item_id int unsigned auto_increment primary key,
purchase_date date
) engine = innodb;
create table computers (
item_id int unsigned primary key,
processor_type varchar(50),
foreign key item_fk (item_id) references items (item_id)
on update restrict on delete cascade
) engine = innodb;
create table printers (
item_id int unsigned primary key,
is_duplex boolean,
foreign key item_fk (item_id) references items (item_id)
on update restrict on delete cascade
) engine = innodb;
Run Code Online (Sandbox Code Playgroud)
作为DBIx :: Class的新手,我想建模数据库实体(计算机和打印机都是项目)之间的继承关系,但是使用提供的belongs_to关系类型,这似乎很尴尬,因为与基类的关联是没有隐藏,所以仍然必须手动为两个类创建实体,并且对派生类中的基类属性的访问不同于访问它们自己的属性.
是否有一个优雅的解决方案,可以让我说:
$printer = $printer_rs->create({purchase_date => $date, is_duplex => 0});
Run Code Online (Sandbox Code Playgroud)
或(在提取的打印机行上):
$date = $printer->purchase_date;
$duplex = $printer->is_duplex;
Run Code Online (Sandbox Code Playgroud)
?
您可以使用proxy属性上的关系,使存取-它的记录中add_relationship的DBIx ::类::和:: Base的,你可以使用它belongs_to,如:
__PACKAGE__->belongs_to(
'item' => 'MyApp::Schema::Item',
'item_id',
{ proxy => [ qw/purchase_date/ ] }
);
Run Code Online (Sandbox Code Playgroud)
这将使您的所有Printer对象具有purchase_date引用相关Item对象的访问器.
至于创造,你不能没有覆盖new_result,这实际上很容易.你只需要利用与创造相关的行为来转向
->create({
is_duplex => 1,
purchase_date => $dt,
})
Run Code Online (Sandbox Code Playgroud)
成
->create({
is_duplex => 1,
item => {
purchase_date => $dt,
},
})
Run Code Online (Sandbox Code Playgroud)
或者你可以item在你的用户身上填写关于哪些列的知识并让他们直接提供hashref;)