我有2个表为1到[0/1]。有没有办法使用Rose::DB::Object自动创建关系对象/行:
例如:
# detailed_summary is the 1-to-1 relationship
# if detailed_summary exist, get it
# if not, create a new one with links?
$obj->detailed_summary
也许是一个触发器?
答案 0 :(得分:1)
column trigger不是你想要的。实现目标的一种方法是使用前导下划线命名您的关系,然后编写自己的无下划线方法来执行“如果它还不存在则创建一个”的事情:
sub detailed_summary
{
my($self) = shift;
my $existing_object = $self->_detailed_summary(@_);
unless($existing_object)
{
# Create a new object
my $new_object = My::Summary->new(...);
# Assign it to its parent so it will be stored in the
# database when the parent is save()d, then return it.
return $self->_detailed_summary($new_object);
}
return $existing_object;
}
你也可以通过在创建后生成的detailed_summary()方法,手动(使用typeglobs和子例程引用),或者使用可以包装现有子例程的CPAN模块来执行相同的操作。
(上面的代码是非常规则的,如果你最终做了这么多,你应该能够自动创建它。)