On 3/10/07, Mike Schilli <[EMAIL PROTECTED]> wrote:
> $foo->add_bars({ string => "I am Bar" });
> $foo->save();
>
> The first time this is run, records for both "I am Foo" in foos and "I am Bar"
> in bars are created [...] and the relationship table contains the mapping:
>
> mysql> select * from foo_bar_map;
> +----+--------+--------+
> | id | foo_id | bar_id |
> +----+--------+--------+
> | 1 | 1 | 1 |
> +----+--------+--------+
>
> But the second time around, I would expect it to reuse the existing mapping,
> which works well for the foos and bars tables, but surprisingly, it adds a
> second identical mapping record:
>
> mysql> select * from foo_bar_map;
> +----+--------+--------+
> | id | foo_id | bar_id |
> +----+--------+--------+
> | 1 | 1 | 1 |
> | 2 | 1 | 1 |
> +----+--------+--------+
>
> Is there a way around that?
If you add the same bar twice, it'll add a new mapping each time.
That's what add_bars() does. If you try to do what Jud suggested:
On 3/11/07, Jud <[EMAIL PROTECTED]> wrote:
> You could add a UNIQUE INDEX(foo_id, bar_id)
All that'll do is die with an error when you try to add the second
one. If what you really want to do is "add a related bar only if that
bar is not already related" then you have to write that out, rather
than simply calling add_bars() multiple times with the same argument.
In your example, it'd be something like:
unless(grep { $_->string eq 'I am Bar' } $foo->bars)
{
$foo->add_bars({ string => "I am Bar" });
}
-John
-------------------------------------------------------------------------
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/rose-db-object