The ManyToOne TransferObject is not persisted.

4 views
Skip to first unread message

Will Swain

unread,
Jul 28, 2008, 5:55:01 PM7/28/08
to transf...@googlegroups.com

Hello,

Have an issue here, and I hope someone can help:

I have a news object and an event object. The news object can be related to
an event object, or maybe not. This is the relevant part of my transfer.xml:

<package name="events">
<!-- event details -->
<object name="Event" table="tbl_events">
<id name="eventID" type="numeric"/>
<property name="event_title" type="string"
column="event_title"/>
<property name="event_details" type="string"
column="event_details"/>
<property name="event_start_date"
type="date" column="event_start_date"/>
<property name="event_end_date" type="date"
column="event_end_date"/>
</object>

</package>

<package name="news">
<!-- news details -->
<object name="News" table="tbl_news">
<id name="newsID" type="numeric"/>
<property name="news_headline" type="string"
column="news_headline"/>
<property name="news_maintext" type="string"
column="news_maintext"/>
<property name="news_image" type="string"
column="news_image"/>
<property name="news_date" type="date"
column="news_date"/>
<property name="archived" type="string"
column="archived"/>
<manytoone name="tEventID">
<link column="tEventID"
to="events.Event" />
</manytoone>
</object>
</package>

When adding a new News item, the user can select an event from a drop down
list. If they don't, a default of 0 is set.

This is my code on the processing page:

<cfscript>
transfer = application.transferFactory.getTransfer();
news = transfer.new("news.News");
Tevent = transfer.get("events.Event", #Form.tEventID#); ---- the
value of Form.tEventID is 0 if an event isn't selected ----
news.setnews_headline(#Form.news_headline#);
news.setnews_maintext(#Form.news_maintext#);
news.setnews_date(#Form.news_date#);
news.setarchived(#Form.archived#);
news.setnews_image(#news_image#);
news.settEventID(#Tevent#);

transfer.save(news);
</cfscript>

I thought that an empty event object would be created if I pass in a value
of 0, which does seem to be the case. However, I get this error:

In TransferObject 'news.News' manytoone 'events.Event' has not been
persisted in the database. The ManyToOne TransferObject is not persisted.

Clearly I'm missing something fundamental or I'm approaching this in the
wrong way? How should I be handling this situation?

Thanks for your time guys.

Will

Nando

unread,
Jul 28, 2008, 8:28:41 PM7/28/08
to transf...@googlegroups.com
I see a few possible problems. One is that is seems you're trying to set an id for the event instead of setting an event object, sort of:

news.settEventID(#Tevent#);

Also, you don't need the hash marks (in any variables within ()), and sett had ttoo many tt's. Try this:

news.setEvent(Tevent);






Second is that I believe you need to use CascadeSave if you want to save the many2one at the same time.
--

Nando M. Breiter
The CarbonZero Project
CP 234
6934 Bioggio
Switzerland
+41 76 303 4477
na...@carbonzero.ch

Mark Mandel

unread,
Jul 28, 2008, 8:42:07 PM7/28/08
to transf...@googlegroups.com
The error is telling you that you are trying to save your news object,
but the event object is unpersisted, so it doesn't know how to save
the news object.

Tevent = transfer.get("events.Event", #Form.tEventID#);

if(Tevent.getIsPersisted())
{
news.setEvent(Tevent);
}

Would be more appropriate.

Yes you get a brand new object when it can't find one, but you still
need to do something with it.

Mark

--
E: mark....@gmail.com
W: www.compoundtheory.com

Will Swain

unread,
Jul 29, 2008, 5:27:32 AM7/29/08
to transf...@googlegroups.com
Ah, ok. I think I understand now. Thanks Mark.

Cheers

Will

Will Swain

unread,
Jul 29, 2008, 5:41:48 AM7/29/08
to transf...@googlegroups.com
Hi Nando,
 
Actually, the database field is called tEventID, so I know it looks odd, but settEventID is correct. I guess this is just a naming legacy of me thinking in database terms and not in object terms - one day my head will switch around I promise!!
 
Thanks for the advice though, and I've cleaned up the code and removed the extraneous #'s
 
Cheers
 
Will


From: transf...@googlegroups.com [mailto:transf...@googlegroups.com] On Behalf Of Nando
Sent: 29 July 2008 01:29
To: transf...@googlegroups.com
Subject: [transfer-dev] Re: The ManyToOne TransferObject is not persisted.

Robert Rawlins

unread,
Jul 29, 2008, 6:03:05 AM7/29/08
to transf...@googlegroups.com

Will, to get around your naming convention issues you could do:

 

<property name="EventID" type="integer" column="tEventID" />

 

This should mean your object property is named EventID and you can then do setEventID() which will likely seem more natural to you. Sometimes it’s nice to map a more friendly property name to an ugly column name.

 

Hope that helps,

 

Robert

<BR

Will Swain

unread,
Jul 29, 2008, 8:37:44 AM7/29/08
to transf...@googlegroups.com
Thanks Robert, that is helpful.
 
I'm making progress I think, in terms of trying to move to a more OO approach, but it's hard going, and I'm finding little things like thinking in terms of database tables and not objects are hard to shake off.


From: transf...@googlegroups.com [mailto:transf...@googlegroups.com] On Behalf Of Robert Rawlins
Sent: 29 July 2008 11:03

Nando

unread,
Jul 29, 2008, 9:46:17 AM7/29/08
to transf...@googlegroups.com
Ah, i see now ... I assumed you would have named the manytoone "Event". The way you named it confused me. :-)

If you simply want to set the property id of Event in News (eventId), you can do away with the manytoone declaration and instead of setting the transfer object "Event" in news, declare the property eventId in News and in your processing template, set the numeric value of the eventId passed from your dropdown in the form. Transfer won't be aware of the relationship, but you can use gateway methods (normal queries) to glue them together where needed.

Sometimes I'm finding it really useful to declare the relationship in Transfer, and other times not so, especially n the case of a dropdown and a manytoone like I assume you have here.

Will Swain

unread,
Jul 30, 2008, 5:46:48 AM7/30/08
to transf...@googlegroups.com
Hi Nando,
 
I've got you. Thanks for the help - much appreciated from this noob to Transfer and all this jazz.


From: transf...@googlegroups.com [mailto:transf...@googlegroups.com] On Behalf Of Nando
Sent: 29 July 2008 14:46
Reply all
Reply to author
Forward
0 new messages