I have a model called “Org”, with an assocation to a model “State”.
Org has a many-to-one association with state:
property name="state" fieldtype="many-to-one" type="array" cfc="State" singularname="state" fkcolumn="state_id";
I have a form to edit and save the Org properties, which I am doing with:
`
savedOrg = populateModel(prc.org).save();
`
My question is… How do I save the state?
Many thanks in advance?
Jason
Prior to 3.7.0, you’ll need to set the property of org, e.g.:
theOrg = populateModel( prc.org ); // set state theOrg.setState( instanceOfState ); // save entity ...
Note that you’ll need to set the State property to an instance of the desired state, not just the id.
Also, just FYI, the “singularname” property is only valid for one-to-many and many-to-many relationships.
Hope this helps!
Thanks Joel… I thought it may be something like that, but I can’t think how to create the instanceOfState populated with the value passed from the form.
I currently have this
`
savedOrg = populateModel(prc.org);
state = getModel(“State”).new(id=rc.stateID);
savedOrg.setState(state).save();
`
… but receiving error the following error:
The passed value does not evaluate to a valid array object
How do I create the state object with the value of the state selected in the form?
Many thanks for your help!!
Jason
To get an instance of the desired state, you could do something like this:
entityNew(“State”).get( rc.stateid )
You’ll also need to remove the “type=array” from your definition (sorry, didn’t notice it before). The “type” attribute is not valid for many-to-one relationships, only one-to-many and many-to-many.
Be sure to check out the Relationship Attributes table in the docs: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WS5FFD2854-7F18-43ea-B383-161E007CE0D1.html#WS0A6DEEDD-1725-48d5-B13E-A3D30AC4E67C