[Dbix-class] Automatically "create" on related tables
Jonas Alves
jonas.alves at gmail.com
Thu Apr 26 23:43:29 GMT 2007
On 26/04/07, Jess Robinson <castaway at desert-island.me.uk> wrote:
>
>
>
> On Tue, 24 Apr 2007, Eduard Giménez wrote:
>
> > Hi,
> >
> > I'm working on a existing DB which has some data spanned across several
> > tables. For example each user has an entry on the table "users" and on the
> > "user_data" table. I know that it's not he best option, but It's how the DB
> > is and I can change it :(
> >
> > Currently to add a new user I do (on Catalyst, btw):
> >
> > $u = $c->model('myDB::users')->create( { email => $email, active => 1,
> > ...});
> > $u->create_related('user_data', {field01 => $field1, field02 => $field02,
> > ...});
> >
> > I would like to do something similar what is shown at [1]
> >
> > $c->model('myDB::users')->create( { email => $email,
> > active => 1,
> > ...
> > user_data => {field01 =>
> > $field1,
> > field02 =>
> > $field02,
> > ...},
> > } );
> >
> >> From that [2] Matt's message I undersant that this feature was going to be
> > merged. Is that true? Has been already merged? On which DBIx::Class version?
> >
> > TIA,
> >
> > [1] http://www.mail-archive.com/[email protected]/msg02624.html:
> > [2] http://www.mail-archive.com/[email protected]/msg02671.html
> >
>
> No, it hasn't.. it's lurking in the bulk_create branch, looking for more
> tuits, and some nice people to write more scary tests to stress it.
>
> What you've shown does work.. its find_or_create type stuff that doesnt
> yet.
>
Here goes a patch to make find_or_create work:
Index: t/96multi_create.t
===================================================================
--- t/96multi_create.t (revision 3214)
+++ t/96multi_create.t (working copy)
@@ -7,7 +7,7 @@
my $schema = DBICTest->init_schema();
-plan tests => 6;
+plan tests => 7;
my $cd2 = $schema->resultset('CD')->create({ artist =>
{ name => 'Fred Bloggs' },
Index: lib/DBIx/Class/ResultSet.pm
===================================================================
--- lib/DBIx/Class/ResultSet.pm (revision 3214)
+++ lib/DBIx/Class/ResultSet.pm (working copy)
@@ -350,11 +350,17 @@
foreach my $key (keys %$input_query) {
if (ref($input_query->{$key})
&& ($info = $self->result_source->relationship_info($key))) {
- my $rel_q = $self->result_source->resolve_condition(
- $info->{cond}, delete $input_query->{$key}, $key
- );
- die "Can't handle OR join condition in find" if ref($rel_q) eq 'ARRAY';
- @related{keys %$rel_q} = values %$rel_q;
+ if (ref($input_query->{$key}) &&
!Scalar::Util::blessed($input_query->{$key})) {
+ # Abusing find
+ delete $input_query->{$key};
+ }
+ else {
+ my $rel_q = $self->result_source->resolve_condition(
+ $info->{cond}, delete $input_query->{$key}, $key
+ );
+ die "Can't handle OR join condition in find" if ref($rel_q)
eq 'ARRAY';
+ @related{keys %$rel_q} = values %$rel_q;
+ }
}
}
if (my @keys = keys %related) {
Cheers,
--
Jonas
More information about the Dbix-class
mailing list