In your example they are using different tables, one is using posttags,and one is using commenttags.
I can't guarentee what will happen if you mix n' match m2m relationships on a single table. Transfer assumes a m2m is a table with 2 foreign keys on it, no more, no less. If you want to go down this route, i would suggest an interim object with composite ids. Mark On Sat, Sep 26, 2009 at 1:50 PM, whostheJBoss <[email protected]>wrote: > > :) Perhaps your brain will see the answer to this one instantly (or > you've got a standard solution for this circumstance). Mine doesn't > want to see it, I guess. > > I am using ColdBox and Transfer and am experiencing some behavior I > can't wrap my head around. (It's most likely an error in my logic) > > I have a Transfer object defined called posts.post which has a many to > many of tags.tag: > > <package name="posts"> > <object name="post" table="posts"> > <id name="postID" type="numeric" /> > <property name="postTitle" type="string" column="postTitle" > nullable="false" /> > > <manytomany name="tags" table="posttags" proxied="true" > lazy="true"> > <post to="posts.post" column="postID"/> > <post to="tags.tag" column="tagID"/> > <collection type="array"> > </collection> > </manytomany> > > </object> > </package> > > <package name="tags"> > <object name="tag" table="tags" > decorator="components.decorators.tags"> > <id name="tagID" type="numeric" /> > <property name="tagTitle" type="string" column="tagTitle" > nullable="false" /> > </object> > </package> > > I also have a Transfer object called comments.comment which also needs > a many to many of the tags.tag object > > Basically, I have posts and comments, both of which have tags. I want > them to share the same tags table. > > In my posttags (the link table) table I have: > > postTagID > tagID > postID > commentID > > So, before I add the comments.comment object to the mix, everything is > fine. I can add tags to my posts.post objects and they are saved in > the database as they should be when I used Transver.save(), everything > works great. The commentID field in the posttags table isn't used at > this point. > > So, if I add: > > <package name="comments"> > <object name="comment" table="comments"> > <id name="commentID" type="numeric" /> > <property name="commentTitle" type="string" > column="commentTitle" > nullable="false" /> > > <manytomany name="tags" table="commenttags" proxied="true" > lazy="true"> > <comment to="comments.comment" column="commentID"/> > <comment to="tags.tag" column="tagID"/> > <collection type="array"> > </collection> > </manytomany> > > </object> > </package> > > This also gives the comments.comment objects a many to many with > tags.tag on commentID (instead of postID) > > So, when viewing these objects on a page, it works fine. I can do: > > instance.Transfer.get("comments.comment", 1) and dump the object's > getTagsArray() and see all of the associated tags. > > I can do: > > instance.Transfer.get("posts.post", 1) and dump the object's > getTagsArray() and see all of the associated tags. > > Both objects work fine for retrieving data. > > So, if I do: > > // get a post > myPost = instance.Transfer.get("posts.post", 1); > > // get two tags > tag1 = instance.Transfer.get("tags.tag", 1); > tag2 = instance.Transfer.get("tags.tag", 2); > > // add the tags to the post > myPost.addTags(tag1); > myPost.addTags(tag2); > > I am just adding two tags to post with ID 1. > > If I dump myPost.getTagsArray() I see both tags appear properly in the > array, however, if I add: > > instance.Transfer.save(myPost); after the two addTags() lines to save > the myPost object and then dump mypost.getTagsArray(), after the save, > the tags are gone. They do not save and do not show up in the database > in the posttags table. > > When I check the SQL output, no inserts have been run. > > Somehow, saving the object drops the many to many relationships it had > right before save. > > If I remove the many to many on the comments.comment object, the > posts.post save begins to work and save the tags and inserts them into > the database. So, having two objects with the same many to many, even > on different fields, causes erratic behavior. > > Obviously I can addTags() fine, they show up in the object, they just > won't save whenever another object also has a many to many with the > same object I'm trying to save. > > As a workaround, I created a second tag object: > > <object name="ctag" table="tags" > decorator="components.decorators.tags"> > <id name="tagID" type="numeric" /> > <property name="tagTitle" type="string" column="tagTitle" > nullable="false" /> > </object> > > Then told the many to many on comments.comment to use that object > instead: > > <manytomany name="tags" table="commenttags" proxied="true" > lazy="true"> > <comment to="comments.comment" column="commentID"/> > <comment to="tags.ctag" column="tagID"/> > <collection type="array"> > </collection> > </manytomany> > > Doing this allows me to add tags to both posts.post objects and > comments.comment object without problems, but it feels like a cheap > way of doing this since I'm using two different tag objects which > represent the same data. > > (One other odd note... If I reinitialize my ColdBox application on the > page that adds tags to and save the posts.post object before calling > any others, it begins to work. After that, I can add all the tags I > want to posts.post and they get saved, even when the comments.comment > many to many for tags.tag also exists. Although, sometimes this only > works the first time I hit the page. However, if I reinitialize my > application on any other page BUT the one that adds and saves tags, > then when I do try to call that page, the tags aren't saved to the > database. They show up on the object after they've been added, but > once I do Transfer.save() on the object, they are no longer there.) > > Perhaps this is some funky cache / object recreation issue, but I'm > not sure at all at this point. I thought maybe whichever object uses > the many to many relationship first (comments.comment or posts.post) > would be the only one that could save the tags, but I'm not sure. I > can still add tags to either one and they show up in the array, but > once I save... poof, they are gone. > > I'm just looking for the "right" way to set this up, where two objects > need to both have a many to many on the same object. > > Like I said, right now I can just use a second tag object definition > on a different column and that seems to resolve things, it just > doesn't "feel" right. Especially if I update the tag's data, then both > tags.tag and tags.ctag would have to be updated, and, well, that's > getting into tricky territory. > > ANY suggestions would help, really just looking for the right way to > structure this. I should be able to deduce the basic logic from > someone saying "Oh, you just need to have many to many on [insert the > answer I need here] instead" =) > > > -- E: [email protected] T: http://www.twitter.com/neurotic W: www.compoundtheory.com --~--~---------~--~----~------------~-------~--~----~ Before posting questions to the group please read: http://groups.google.com/group/transfer-dev/web/how-to-ask-support-questions-on-transfer You received this message because you are subscribed to the Google Groups "transfer-dev" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/transfer-dev?hl=en -~----------~----~----~----~------~----~------~--~---
