1 対 [0/1] の 2 つのテーブルがあります。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
もしかしてトリガー?
1 対 [0/1] の 2 つのテーブルがあります。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
もしかしてトリガー?
列トリガーは必要なものではありません。目標を達成する 1 つの方法は、先頭にアンダースコアを付けてリレーションシップに名前を付け、「まだ存在しない場合は作成する」ことを行う独自のアンダースコアなしのメソッドを作成することです。
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 モジュールを使用して、同じことを行うこともできます。
(上記のコードは非常に規則的であり、これを頻繁に行う場合は、作成を自動化できるはずです。)