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?

> 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" });
>     }

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.

Another approach would be to 1) select the record from bars and 2)
select the record from the mapping table, which would be faster in case
there's many bars for a given foo.

Or, you could join the mapping table and the bars table to figure it
out with a single select.

In a 1:1 relationship, if you do

    $foo->bar({ string => "I am Bar" });

and the bars record doesn't exist yet, Rose does

  SELECT bar_id, id, string FROM foos WHERE string = ? - bind params: I am Foo
  SELECT id, string FROM bars WHERE string = ? - bind params: I am Bar
  INSERT INTO bars
  ( id, string) VALUES ( ?, ?) - bind params: , I am Bar
  INSERT INTO foos
  ( bar_id, id, string) VALUES ( ?, ?, ?) - bind params: 1, , I am Foo

If the bars record exists, however, Rose does

  SELECT bar_id, id, string FROM foos WHERE string = ? - bind params: I am Foo
  SELECT id, string FROM bars WHERE string = ? - bind params: I am Bar
  UPDATE foos SET bar_id = ?, string = ?
    WHERE id = ? - bind params: 1, I am Foo, 1

which is exactly the right thing to do.

Could something along these lines be added to the many-to-many
relationship handling?

-- Mike

Mike Schilli
[EMAIL PROTECTED]

>
> -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
> 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

Reply via email to