The error makes sense as it seems that you're trying to save the children "graphicText" before the parent "graphic" is saved.
I'd save the parent before the children and wrap the whole thing up in a transaction if you need it to be atomic. - Gabriel On Mon, Nov 2, 2009 at 10:34 AM, marc <[email protected]> wrote: > > Hi, > I want to add a record in a related table but the parent record is not > persisted. I get the error > > Error Messages: The Parent OneToMany TransferObject is not persisted. > In TransferObject 'CMS.graphicText' onetomany parent 'CMS.graphic' has > not been persisted in the database. > > I have 2 tables: graphic, containing properties of graphics, and > graphictexts, containing texts of graphics. Each record in table > graphicTexts points back to a record in table graphic. > > This is my transfer.xml: > > <object name="graphic" table="graphics"> > <id name="id" type="numeric"/> > <property name="menuid" type="numeric" column="FK_Menuid" > nullable="false" /> > <property name="title" type="string" column="title" nullable="false" > / > > > <property name="clientFileName" type="string" > column="clientFileName" > nullable="false" /> > <property name="serverFileName" type="string" > column="serverFileName" > nullable="false" /> > <property name="fileSize" type="numeric" column="fileSize" > nullable="false" /> > <property name="contentInfo" type="string" column="contentInfo" > nullable="true" /> > <property name="width" type="numeric" column="width" > nullable="false" /> > <property name="height" type="numeric" column="height" > nullable="false" /> > <property name="maxWidth" type="numeric" column="maxWidth" > nullable="false" /> > <property name="showDateFrom" type="boolean" column="showdateFrom" > /> > <property name="dateFrom" type="date" column="dateFrom" > nullable="true" /> > <property name="showDateTo" type="boolean" column="showDateTo" /> > <property name="dateTo" type="date" column="dateTo" nullable="true" > / > > > <!-- Link between a graphic and it's texts--> > <onetomany name="graphicTexts"> > <link to="CMS.graphicText" column="FK_graphic"/> > <collection type="array"> > <order property="id" order="asc"/> > </collection> > </onetomany> > <object name="graphic" table="graphics"> > > and this linked object > > <object name="graphicText" table="graphicTexts"> > <id name="id" type="numeric"/> > <property name="tooltip" type="string" column="tooltip" > nullable="true" /> > <property name="alttext" type="string" column="alttext" > nullable="true" /> > <property name="subscript" type="string" column="subscript" > nullable="true" /> > <manytoone name="language"> > <link to="Language.language" column="FK_language"/> > </manytoone> > </object> > > If I submit a form and make a new object using the form's data I have > this code > > <cfset local.oGraphic=transfer.new("CMS.graphic")> > > [... code ..] > > <!---remove existing texts---> > <cfset local.graphictexts=local.oGraphic.getGraphicTextsArray()> > <cfloop array="#local.graphictexts#" index="local.graphictext"> > <cfset transfer.delete(local.graphictext)> > </cfloop> > <cfset local.qLanguages=transfer.list("Language.language")> > <cfloop query="local.qLanguages"> > <!---create a graphicText object for each alttext,subscript > or > tooltip that is entered---> > <cfif StructKeyExists(arguments.formdata,"altext_#title#") > OR > StructKeyExists(arguments.formdata,"tooltip_#title#") OR > StructKeyExists(arguments.formdata,"subscriptGraphic_#title#")> > <cfset > local.oGraphicText=transfer.new("CMS.graphicText")> > <cfset > local.oGraphicText.setlanguage(transfer.readByProperty > ("Language.language","title",title))> > <cfset > local.oGraphicText.setAltText(Iif(StructKeyExists > (arguments.formdata,"ALText_#title#"),"arguments.formdata > ['ALText_#title#']","''"))> > <cfset > local.oGraphicText.setTooltip(Iif(StructKeyExists > (arguments.formdata,"tooltip_#title#"),"arguments.formdata > ['tooltip_#title#']","''"))> > <cfset > local.oGraphicText.setSubscript(Iif(StructKeyExists > (arguments.formdata,"subscriptGraphic_#title#"),"arguments.formdata > ['subscriptGraphic_#title#']","''"))> > <cfset > local.oGraphicText.setParentGraphic(local.oGraphic)> > <cfset transfer.save(local.oGraphicText)> > </cfif> > </cfloop> > > <!---set extra info in textobject and return it---> > <cfset local.oGraphic=setExtraInfo > (CMSObject=local.oGraphic,formData=arguments.formData)> > <cfset transfer.save(local.oGraphic)> > [... code..] > > I get the error on this line > > <cfset transfer.save(local.oGraphicText)> > > After a while I figured out why: when I create a new object. It's Id > is 0 since it's not persisted yet. That's ok when only setting > properties of the object but is a problem when I want to store a > record in a related table. That related record (local.oGraphicTexts) > needs the ID value of the parent record (local.oGraphic) since it's a > onetomany relationship. The parent record (in table local.oGraphic) is > not yet persisted so has no Id. > > Should I just save the new record before setting any property or is > there a better solution for this? > > Thanks, > > Marc > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
