Maybe I'm missing something here, because I'm not seeing where the
problem is:
In sort-of pseudo-code:
<cfscript>
Book = transfer.get("book",0); // returns the same thing as
transfer.new("book");
Theme = transfer.get("theme",11); // returns the TO for ID 11
BookTheme = transfer.new("booktheme");
Book.setTitle("Ich Bin Ein Book!");
// set the rest of the book's required fields here
BookTheme.setStarRating(4);
BookTheme.setParentBook(Book);
BookTheme.setParentTheme(Theme);
transfer.cascadeSave(BookTheme);
</cfscript>
Where's the issue? cascadeSave() HAS to go thru and save any
uncommitted composed objects (i.e. Book) before it saves the root
object (i.e. BookTheme) because it can't save the rest of the related
objects until it has the primary key values for for the uncommitted
objects. All TransferObjects have a getIsPersisted() method on them
that can be used to tell if an object is saved or not. I might be
wrong on this, because I have never used composite keys, but I have
used cascadeSave() in many cases and it handles a mix of unpersisted,
clean and dirty objects just fine.
It wouldn't make a lot of sense to me if that changed just because
you are using composite keys.
Having just taken a quick read thru the cascadeSave() mechanism in
Transfer, it saves the top-level object last, only after spinning
thru the whole rest of the object tree and saving them first. That
way, if you have no book ID when you call cascadeSave(BookTheme),
you'll have one before it tries to persist the BookTheme.
You're safe just using something like the code above.
J
On Oct 28, 2008, at 9:17 AM, Stephen Moretti wrote:
> I really don't understand why, but there is something about
> composite keys that just isn't sinking in and I don't understand
> why its not working.
>
> I have the following objects in a package called stories:
>
> <object name="book" table="book">
> <id name="ID" column="BookID" type="numeric"/>
> <property name="Title" type="string"/>
> <property name="Summary" type="string"/>
> <property name="Author" type="string"/>
> <property name="Illustrator" type="string" nullable="true"/>
> <property name="DateCreated" type="date"/>
> <property name="DatePublished" type="date" nullable="true"/>
> <property name="BookImage" type="string"/>
> <onetomany name="Theme" lazy="true">
> <link to="stories.booktheme" column="BookID" />
> <collection type="array">
> </collection>
> </onetomany>
> </object>
>
> <object name="booktheme" table="booktheme">
> <compositeid>
> <parentonetomany class="stories.book" />
> <parentonetomany class="stories.theme" />
> </compositeid>
> <property name="StarRating" type="numeric" />
> </object>
>
> <object name="theme" table="theme">
> <id name="ID" column="ThemeID" type="numeric"/>
> <property name="Title" type="string"/>
> <property name="UniqueName" type="string"/>
> <onetomany name="Book" lazy="true">
> <link to="stories.booktheme" column="ThemeID" />
> <collection type="array">
> </collection>
> </onetomany>
> </object>
>
> I can now update book properties and add new Themes with star
> ratings to books, but I'm a little uncomfortable about the adding
> themes to a new book and a unsure about updating themes on an
> existing book.
>
> The code I have for adding themes to a book is as follows :
>
> locals.tBookTheme = getContentServer().getNew("stories.booktheme");
> locals.themeID = ListLast(locals.thisField,"_");
> locals.tTheme = getContentServer().getByID
> ("stories.theme",locals.themeID);
> locals.tBookTheme.setParentBook(bookClone);
> locals.tBookTheme.setParentTheme(locals.tTheme);
> locals.tBookTheme.setStarRating(locals.thisFieldValue);
> getContentServer().save(locals.tBookTheme);
>
>
> getContentServer() basically equates to being Transfer, but with
> added extras.
> The star rating is held in a form field called starrating_
> {themeid}, so I get the themeID from the form field name and then
> get a theme TO for that themeID. The current book, bookClone, can
> either be a clone of an existing book or a new book TO.
>
> What I'm uncomfortable with when adding themes to a new book is
> that at the point that themes and their star ratings are being
> added to a book TO there is no bookID, so if I save this book theme
> TO there won't be a bookID or the BookID will be zero. However, if
> I don't save the book theme and its star rating I'm not sure how I
> can add the book theme to a book TO so that I can then run a
> casadeSave() against my book TO.
>
> If I try to update my array of book theme and their star ratings I
> get a problem that having emptied the theme array on my book TO I
> have to cascadeSave() the whole book TO. This is so that when I
> save a book theme I don't get a duplicate key error. I don't really
> like saving the whole record at this point. I only want to save my
> object to the database when everything is ready and not before.
> If you're wondering why I'm emptying the theme array, its because
> there's really not that many themes. There's more processing
> involved in checking to see if an individual theme currently exists
> adding it and deleting any themes that shouldn't be in there any
> more than to simply dump the lot and reinsert the records.
>
> The problem boils down to - how do I add child book themes to a
> book TO without saving the book TO or the booktheme TO?
>
> Does that make any sense? Am I going about this the wrong way?
> What am I missing?
>
> Thanks for your help
>
> Stephen
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---