<p>When you assign element IDs to a relation field you need to pass the data in an array format.</p>

<p>See this related answer: <a href="https://craftcms.stackexchange.com/questions/1264/in-a-plugin-how-do-i-save-an-entry-with-a-related-entry">In a plugin, how do I save an entry with a related entry</a></p>

<p>Key information in that answer:</p>

<pre><code>&lt;input type="text" name="fields[customTextField]" value="My Text Data"&gt; 
&lt;input type="text" name="fields[customRelationField][]" value="101"&gt;
</code></pre>

<p>Note that in the input field name of 'customRelationField' relations field we are passing an array: <code>fields[customRelationField][]</code>.</p>

<p>Also note the relation field data is a full replace. So if you ever want to add an element ID to an existing relation field that has relation already set, you need to get the existing IDs as an array and add your additional IDs when updating the field.</p>

CC BY-SA 3.0
2016-09-29T15:38:10.560Z
That fixed the issue. Thanks very much for your help
Bryan L. Fordham
CC BY-SA 3.0
2016-09-29T14:49:59.627Z
2016-09-29T14:49:59.627Z
2017-04-13T12:51:00.200Z
Luke Holder
<p>I'm trying to use custom order fields in Craft Commerce to store a reference to an object, but all I am getting back is an ElementCriteria model. </p>

<p>I have two custom order fields defined: <code>twoDayOption</code> and <code>fourDayOption</code>. Both are events from Solspace Calendar (<code>Calendar_EventElementType</code>).</p>

<p>In the Commerce -> Orders page, I can set these fields with no problem.</p>

<p>On my product page I have a dropdown for selecting the values of each of these fields. They look pretty much the same. Here is the <code>twoDayOption</code> one:</p>

<pre><code>&lt;select name="fields[twoDayOption]"&gt;
    {%- for days in craft.calendar.events({calendar: 'workshops'}) -%}
        &lt;option value="{{ days.id }}"&gt;{{ days.title }}&lt;/option&gt;
    {%- endfor -%}
&lt;/select&gt;
</code></pre>

<p>After submitting the form the values for <code>twoDayOption</code> and <code>fourDayOption</code> seem to be lost. When I call <code>{{ dump( cart.twoDayOption) }}</code> I get an object of type <code>Craft\ElementCriteriaModel</code>. It references the <code>Calendar_EventElementType</code>, but the ID that was submitted is not there.</p>

<p>Here's the first part of the output of that dump:</p>

<pre><code>object(Craft\ElementCriteriaModel)#757 (17) {
  ["strictAttributes":protected]=&gt;
  bool(false)
  ["_elementType":"Craft\ElementCriteriaModel":private]=&gt;
  object(Craft\Calendar_EventElementType)#990 (7) {
    ["componentType":protected]=&gt;
    string(11) "ElementType"
    ["_sourcesByContext":"Craft\BaseElementType":private]=&gt;
    NULL
    ["_classHandle":"Craft\BaseComponentType":private]=&gt;
    NULL
[snip]
</code></pre>

<p>Looking through <code>Commerce_CraftController</code>, I found that it calls <code>BaseElementModel-&gt;setContentFromPost("fields")</code> to set the custom order fields. I added in some logging statements to make sure it was getting the information I expected.</p>

<p>When it enters the method it finds these values from <code>$_POST['fields']</code>:
    Array
    (
        [twoDayOption] => 16
        [fourDayOption] => 12
    )</p>

<p>This is exactly whe I expect. It then finds the two custom field handles that match this. I have other logging statements that show what handle it's processing, and the value it gets from the POST:</p>

<pre><code>2016/09/29 13:54:39 [info] [application] setContentFromPost twoDayOption
2016/09/29 13:54:39 [info] [application] setContentFromPost twoDayOption: 16
</code></pre>

<p><code>setContentFromPost</code> also gets the <code>fieldType</code>, and calls <code>$fieldType-&gt;prepValueFromPost</code>. The value 16 is passed in, and 16 is returned.</p>

<p>Finally, that value is put into <code>$this-&gt;_content-&gt;$handle</code>, and I have a logging statement immediately after that verifies I can retrieve 16 from it. But when I log <code>$this-&gt;twoDayOption</code>, I'm getting the <code>ElementCriteriaModel</code> again.</p>

<p>I'm at a loss. It seems to know it's supposed to have a Calendar Event for that field. Should I not be passing in the ID? Is there another step I am missing?</p>

<plugin-craftcommerce><custom-field>
Unable to store Model in Commerce Order Fields
# Unable to store Model in Commerce Order Fields

(Asked by: username_0 on 2016-09-29)

I'm trying to use custom order fields in Craft Commerce to store a reference to an object, but all I am getting back is an ElementCriteria model. 
I have two custom order fields defined: twoDayOption and fourDayOption. Both are events from Solspace Calendar (Calendar_EventElementType).
In the Commerce -> Orders page, I can set these fields with no problem.
On my product page I have a dropdown for selecting the values of each of these fields. They look pretty much the same. Here is the twoDayOption one:

    {%- for days in craft.calendar.events({calendar: 'workshops'}) -%}
        {{ days.title }}
    {%- endfor -%}


After submitting the form the values for twoDayOption and fourDayOption seem to be lost. When I call {{ dump( cart.twoDayOption) }} I get an object of type Craft\ElementCriteriaModel. It references the Calendar_EventElementType, but the ID that was submitted is not there.
Here's the first part of the output of that dump:
object(Craft\ElementCriteriaModel)#757 (17) {
  ["strictAttributes":protected]=>
  bool(false)
  ["_elementType":"Craft\ElementCriteriaModel":private]=>
  object(Craft\Calendar_EventElementType)#990 (7) {
    ["componentType":protected]=>
    string(11) "ElementType"
    ["_sourcesByContext":"Craft\BaseElementType":private]=>
    NULL
    ["_classHandle":"Craft\BaseComponentType":private]=>
    NULL
[snip]

Looking through Commerce_CraftController, I found that it calls BaseElementModel->setContentFromPost("fields") to set the custom order fields. I added in some logging statements to make sure it was getting the information I expected.
When it enters the method it finds these values from $_POST['fields']:
    Array
    (
        [twoDayOption] => 16
        [fourDayOption] => 12
    )
This is exactly whe I expect. It then finds the two custom field handles that match this. I have other logging statements that show what handle it's processing, and the value it gets from the POST:
2016/09/29 13:54:39 [info] [application] setContentFromPost twoDayOption
2016/09/29 13:54:39 [info] [application] setContentFromPost twoDayOption: 16

setContentFromPost also gets the fieldType, and calls $fieldType->prepValueFromPost. The value 16 is passed in, and 16 is returned.
Finally, that value is put into $this->_content->$handle, and I have a logging statement immediately after that verifies I can retrieve 16 from it. But when I log $this->twoDayOption, I'm getting the ElementCriteriaModel again.
I'm at a loss. It seems to know it's supposed to have a Calendar Event for that field. Should I not be passing in the ID? Is there another step I am missing?

**Tags:** plugin-craftcommerce, custom-field


---

## 1 answers

### Answer by username_1 on 2016-09-29. Score: 4

When you assign element IDs to a relation field you need to pass the data in an array format.
See this related answer: In a plugin, how do I save an entry with a related entry
Key information in that answer:
 


Note that in the input field name of 'customRelationField' relations field we are passing an array: fields[customRelationField][].
Also note the relation field data is a full replace. So if you ever want to add an element ID to an existing relation field that has relation already set, you need to get the existing IDs as an array and add your additional IDs when updating the field.

> **Comments:**
> **username_0**: That fixed the issue. Thanks very much for your help  

---

