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