How to prepare data to write it during only one circle?

24 views
Skip to first unread message

bvn13

unread,
Feb 6, 2012, 2:35:40 PM2/6/12
to rose-db...@googlegroups.com
Hello, help please.

I'm going to write multiple rows into 2 tables of my DB using Rose::DB.
I.e., structures of tables are:

1) table Parents:
a) column id,
b) column name,
c) column child -> link to table Kids::id

2) table Kids:
a) column id,
b) column name

All the keys is OK, forget them please.

Can I prepare all the data (i.e. 2-3 Parents with 1-2 Kids) to write it in one circle for/while?
I read something about "linked initialization" of Rose::DB::Objects, but I don't 100% understood, how it works.
But I think my case is somewhere near.

I mean something like this:

my @data = ();
$data[0] = Persons->new(
   'name' => "mr. Garret, the husband',
   'child' => Kids->new('name' => 'Betty')
);
$data[1] = Persons->new(
   'name' => "mrs. Garret, the wife",
   'child' => Kids->new('name' => 'Betty') #yes, only one child is into DB
);

foreach my $person (@data) {
   $person->save(cascade => 1);
}

I really don't understand, how it works in that case?
Will there be a multiple rows into table Kids in that case or there will be only one row? 
What will be written into DB after that code in case that the DB is empty?

John Siracusa

unread,
Feb 6, 2012, 3:47:00 PM2/6/12
to rose-db...@googlegroups.com
On Mon, Feb 6, 2012 at 2:35 PM, bvn13 <mail...@gmail.com> wrote:
> my @data = ();
> $data[0] = Persons->new(
>    'name' => "mr. Garret, the husband',
>    'child' => Kids->new('name' => 'Betty')
> );
> $data[1] = Persons->new(
>    'name' => "mrs. Garret, the wife",
>    'child' => Kids->new('name' => 'Betty') #yes, only one child is into DB
> );
>
> foreach my $person (@data) {
>    $person->save(cascade => 1);
> }
>
> I really don't understand, how it works in that case?
> Will there be a multiple rows into table Kids in that case or there will be
> only one row?
> What will be written into DB after that code in case that the DB is empty?

Is "name" a unique key in the Kids class? If so, the first save
should create the record for Betty and the second save should look up
Betty by name and use its id.

Setting debugging variables will print the SQL that gets run:

$Rose::DB::Object::Debug = 1;
$Rose::DB::Object::Manager::Debug = 1;

-John

dstroma

unread,
Feb 10, 2012, 8:59:53 PM2/10/12
to Rose::DB::Object
On Feb 6, 2:35 pm, bvn13 <mail4...@gmail.com> wrote:
> I mean something like this:
>
> my @data = ();
> $data[0] = Persons->new(
>    'name' => "mr. Garret, the husband',
>    'child' => Kids->new('name' => 'Betty')
> );
> $data[1] = Persons->new(
>    'name' => "mrs. Garret, the wife",
>    'child' => Kids->new('name' => 'Betty') #yes, only one child is into DB
> );

Logically speaking, it doesn't make sense to call Kids->new to create
two objects when you want to only create one.

This should work shouldn't it?

my $kid = Kids->new('name' => 'Betty');
my @data = ();
$data[0] = Persons->new(
'name' => "mr. Garret, the husband',
'child' => $kid
);
$data[1] = Persons->new(
'name' => "mrs. Garret, the wife",
'child' => $kid
);
Reply all
Reply to author
Forward
0 new messages