Re: Question about creation of records on many tables

[prev] [thread] [next] [Date index for 2004/09/23]

From: Tim Bunce
Subject: Re: Question about creation of records on many tables
Date: 09:35 on 23 Sep 2004
On Wed, Sep 22, 2004 at 11:43:26AM -0700, Zhuang Li wrote:
> It seems the hash should be:
> $user = { name => 'Paul',
> 	     Address => {
> 	  street => 'Homelane',
> 	  number => '56'
> 	     },
> 	};
> 
> Then MyClass::Users->create($user).

The address element needs to be the id of the address record.
Since Class::DBI objects stringify to the id you can do:

  $user = {
    name => 'Paul',
    address => MyClass::Address->create({ street => 'Homelane', number => '56' }),
  };

But the default create() method won't do it automatically.
However, that would be an interesting addition.

Tim.

> john
> 
> 
> -----Original Message-----
> From: Andreas Fromm [mailto:Andreas.Fromm@xxxxxx.xxxxxxxxxxxx.xx] 
> Sent: Wednesday, September 22, 2004 3:05 AM
> To: cdbi-talk@xxxxxx.xxxxx.xxx
> Subject: Question about creation of records on many tables
> 
> Hi,
> 
> Please consider the following Database
> 
> CREATE TABLE Users (
>     id		serial PRIMARY KEY,
>     name	text,
>     address	int(4) references Addresses
> );
> 
> 
> CREATE TABLE Adresses (
>     id		serial PRIMARY KEY,
>     street	text,
>     number	text,
> );
> 
> Is it possible to set the CDBI-classes in such a way the if I pass a
> hash containing the data for a new user with adress e.g.
> 
> $user = { name => 'Paul',
> 	  street => 'Homelane',
> 	  number => '56'
> 	};
> 
> to the create method of Users like in
>    MyClass::Users->( $user );
> 
> I get the correct behavior that the adress-record is created first and
> the id is used in the user-record, all by itself, or do I have to take
> care to create the address-record first?
> 
> Thanks in advance.
> 
> Andreas Fromm
> 
> 
> 
> 
> 

Question about creation of records on many tables
Andreas Fromm 10:05 on 22 Sep 2004

Re: Question about creation of records on many tables
Tim Bunce 09:35 on 23 Sep 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52