使用 Rose::DB::Object,如果找不到,如何在关系中自动创建对象?

发布于 2024-08-21 03:09:23 字数 356 浏览 6 评论 0原文

我有 2 个 1-to-[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

也许是一个触发器?

I have 2 tables that are 1-to-[0/1]. Is there a way to auto create the relationship object/row using Rose::DB::Object:

For example:

# 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

Maybe a trigger?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请叫√我孤独 2024-08-28 03:09:23

列触发器< /a> 不是你想要的。实现目标的一种方法是用前导下划线命名您的关系,然后编写自己的无下划线方法来执行“如果尚不存在则创建一个”的操作:

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()方法,可以手动(使用类型团和子例程引用),也可以使用可以包装现有子例程的CPAN模块。

(上面的代码非常常规,如果您最终经常这样做,您应该能够自动创建它。)

A column trigger is not what you want. One way to accomplish your goal would be to name your relationships with leading underscores and then write your own underscore-less methods to do the "make one if it doesn't yet exist" thing:

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;
}

You could also do the same thing by wrapping the generated detailed_summary() method after it's created, either manually (using typeglobs and subroutine references), or with a CPAN module that can wrap existing subroutines.

(The code above is quite regular and you should be able to automate its creation if you end up doing this a lot.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文