On 7/29/06 3:53 PM, Kurt Hansen wrote:
> package Basket;
> [...]
> relationships => [
> persons => {
> type => 'one to many',
> class => 'Person',
> column_map => { Item => 'Item' },
> },
> [...]
> I'd enter one entry with a transaction_id=10 and Item=1 just fine. If I
> entered another entry with transaction_id=11 and Item=1, the previous
> entry in the "persons" table for transaction_id=10 would be deleted.
> This is because when the save() is executed, the SQL created issues a
> DELETE before the INSERT into "persons", e.g.
>
> DELETE FROM persons WHERE
> Item = ? - bind params: 1
>
> I was very suprised by this DELETE.
When you say "I'd enter one entry" and "I entered another entry", I'm
guessing that you mean this, in code:
$b = Basket->new(...);
$p1 = Person->new(...);
$p2 = Person->new(...);
$b->persons($p1); # "I'd enter one entry"
$b->persons($p2); # "I entered another entry"
What that code actually says, semantically, is this:
# Set the entire list of associated Person objects for the
# Basket object $b to consist of the lone Person object $p1
$b->persons($p1);
# Set the entire list of associated Person objects for the
# Basket object $b to consist of the lone Person object $p2
$b->persons($p2);
So the DELETE is actually appropriate. If what you really want to do is
*add* to the list of Person objects, use either the "add_now" or
"add_on_save" methods of the one-to-many relationship. The "add_on_save"
method type is part of the default_auto_method_types for the OneToMany
relationship, so I'll use that one:
$b = Basket->new(...);
$p1 = Person->new(...);
$p2 = Person->new(...);
# Set the entire list of associated Person objects for the
# Basket object $b to consist of the lone Person object $p1
$b->persons($p1);
$b->save; # db updated here
# Add $p2 to the list of Person objects associated with
# the Basket object $b.
$b->add_persons($p2);
$b->save; # db updated here
For more information on the methods created for each relationship, look at
the "method map" section of the relationship docs and follow the links to
the descriptions of the actual methods:
I discovered this by using the debug
> mode for RDBO:
>
> local $Rose::DB::Object::Debug = 1;
>
> print query; # Just before the save()
>
> The fix was to add the transaction_id to the column_map, i.e.
>
> column_map => { Item => 'Item', transaction_id => 'transaction_id' },
>
> so the DELETE becomes:
>
> DELETE FROM persons WHERE
> Item = ? AND
> transaction_id = ? - bind params: 1, 11
>
> which doesn't match fortunately.
>
> The db rms I'm using is MySQL.
>
> I hope this helps someone.
>
> Take care,
>
> Kurt Hansen
>
>
>
>
>
>
>
> -------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to share your
> opinions on IT & business topics through brief surveys -- and earn cash
> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
> _______________________________________________
> Rose-db-object mailing list
> Rose-db-object@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/rose-db-object
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object