On Sat, Nov 15, 2008 at 9:12 PM, Matt S Trout <[EMAIL PROTECTED]> wrote:
> On Fri, Nov 07, 2008 at 09:51:45AM +0100, Zbigniew Lukasiak wrote:
>> I know this is again not very usual setup - but I provide this info -
>> so that you can make an informed decision about how much
>> back-compatibillity you are going to support for the next version of
>> DBIC. In the past insert worked only on the record passed to it -
>> now it recurses to related records. This fixes some multi-create
>> scenarios - but might be sometimes unexpected.
>
> I see. A suitable failing test patch would be appreciated; we should
> easily be able to modify multi-create to only cascade insert for things
> passed to new()
In the attached no_recursive_insert.diff you'll find the failing test.
For it to pass in the current CPAN release you need to uncomment the
line marked with "For exact backward compatibility" - in the svn
version this additional line would do nothing ( and this is OK IMHO).
I am also attaching a tentative patch to insert in DBIx::Class::Row
and create in DBIx::Class::ResultSet in no_recursive_insert_Row.diff.
It is just a proposal.
--
Zbigniew Lukasiak
http://brudnopis.blogspot.com/
http://perlalchemy.blogspot.com/
Index: t/66relationship.t
===================================================================
--- t/66relationship.t (revision 5141)
+++ t/66relationship.t (working copy)
@@ -7,7 +7,7 @@
my $schema = DBICTest->init_schema();
-plan tests => 69;
+plan tests => 72;
# has_a test
my $cd = $schema->resultset("CD")->find(4);
@@ -261,6 +261,19 @@
ok($new_artist->in_storage, 'artist inserted');
ok($new_related_cd->in_storage, 'new_related_cd inserted');
+my $new_cd = $schema->resultset("CD")->new_result({});
+my $new_related_artist = $new_cd->new_related('artist', { 'name' => 'Marillion',});
+eval {
+ $new_related_artist->insert;
+ $new_cd->title( 'Misplaced Childhood' );
+ $new_cd->year ( 1985 );
+# $new_cd->artist( $new_related_artist ); # For exact backward compatibility
+ $new_cd->insert;
+};
+is ($@, '', 'Reversed staged insertion successful');
+ok($new_related_artist->in_storage, 'related artist inserted');
+ok($new_cd->in_storage, 'cd inserted');
+
# check if is_foreign_key_constraint attr is set
my $rs_normal = $schema->source('Track');
my $relinfo = $rs_normal->relationship_info ('cd');
Index: lib/DBIx/Class/Row.pm
===================================================================
--- lib/DBIx/Class/Row.pm (revision 5141)
+++ lib/DBIx/Class/Row.pm (working copy)
@@ -220,7 +220,7 @@
=cut
sub insert {
- my ($self) = @_;
+ my ($self, $recursive) = @_;
return $self if $self->in_storage;
my $source = $self->result_source;
$source ||= $self->result_source($self->result_source_instance)
@@ -303,10 +303,12 @@
foreach my $obj (@cands) {
$obj->set_from_related($_, $self) for keys %$reverse;
my $them = { %{$obj->{_relationship_data} || {} }, $obj->get_inflated_columns };
- if ($self->__their_pk_needs_us($relname, $them)) {
- $obj = $self->find_or_create_related($relname, $them);
- } else {
- $obj->insert();
+ if( $recursive ){
+ if ($self->__their_pk_needs_us($relname, $them)) {
+ $obj = $self->find_or_create_related($relname, $them);
+ } else {
+ $obj->insert();
+ }
}
}
}
Index: lib/DBIx/Class/ResultSet.pm
===================================================================
--- lib/DBIx/Class/ResultSet.pm (revision 5141)
+++ lib/DBIx/Class/ResultSet.pm (working copy)
@@ -1784,7 +1784,7 @@
my ($self, $attrs) = @_;
$self->throw_exception( "create needs a hashref" )
unless ref $attrs eq 'HASH';
- return $self->new_result($attrs)->insert;
+ return $self->new_result($attrs)->insert( 1 );
}
=head2 find_or_create
_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/[email protected]