On 3/24/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> On Fri, 23 Mar 2007, John Siracusa wrote:
>> If you add the same bar twice, it'll add a new mapping each time.
>> That's what add_bars() does.
>
> Given that Rose knows that this is a many-to-many relationship between
> foos and bars, isn't it pretty far-fetched that you would actually
> want duplicate records in the mapping table?
Not really. A common scenario is if the mapping table has a timestamp
column that defaults to the current time. Adding an item to the
collection makes perfect sense in that context, even if the "same"
item is already in the collection (for whatever value of "same" you
have in your head, which is another thing that may vary greatly from
situation to situation...)
>> 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" });
>> }
>
> That would work, but $foo->bars() could potentially return a lot
> of relationships, and in this case I'm only interested in a single
> one to check if it exists or not.
Yeah, the upcoming addition of a "find" method type for many-to-many
relationships is a better option. Once that arrives (probably next
release) you'd do:
unless(@{ $foo->find_bars({ string => 'I am Bar' }) })
{
$foo->add_bars({ string => 'I am Bar' });
}
which will do exactly what you want: check for a particular related
row, and add the item only if it's not already related.
-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