<p>Related elements, such as categories, are stored by Craft as arrays, even if there is only a single element to be stored.</p>

<p>In your entry form, you don't appear to be storing an array, but only the id of the selected category:</p>

<pre><code>&lt;input type="hidden" name="items[{{ index }}][finish]" value="{{ finish.id }}"&gt;
</code></pre>

<p>This seems to be borne out by what you see when you dump the <code>item</code> variable in your email template. <code>item[finish]</code> should be an array, even if only a single category is assigned to it.</p>

<p>Try using the following in your entry form to see if that helps, which should store the category id as an array:</p>

<pre><code>&lt;input type="hidden" name="items[{{ index }}][finish][]" value="{{ finish.id }}"&gt;
</code></pre>

<p>You should then be able to access the correct value for the category title using the code in <a href="https://craftcms.stackexchange.com/a/12806/153">@carlcs answer</a>.</p>

CC BY-SA 3.0
2015-12-10T16:00:30.863Z
2015-12-10T16:00:30.863Z
2017-04-13T12:51:00.253Z
Steve Rowling
<p>I don't think it has something to do with your event listener, and you just access the related elements not quite right. Try to get them like so:</p>

<pre><code>{% set finish = item.finish.first() %}
{% if finish %}
    &lt;h3 class="h h6 caps--spaced m-b--05"&gt;Finish&lt;/h3&gt;
    &lt;p class="basket-bits"&gt;{{ finish.title }}&lt;/p&gt;
{% endif %}
</code></pre>

<p>or use a <code>relatedTo</code> query to get the finishes form the field.</p>

CC BY-SA 3.0
2015-12-10T13:44:52.090Z
Using `{{ entry.finish.first().title }}` gave me:

`Impossible to access an attribute ("title") on a null variable`

Tried using `{{ item.finish }}` and it gives me the following:

`Object of class Craft\ElementCriteriaModel could not be converted to string`

But using `{{ item.finish[0] }}` gives me

`Key "0" in object with ArrayAccess of class "Craft\ElementCriteriaModel" does not exist`
Luke Pearce
CC BY-SA 3.0
2015-12-10T15:00:31.200Z
Yep, sorry. It's the `item` variable, not `entry`. Updated the answer.
carlcs
CC BY-SA 3.0
2015-12-10T15:13:14.427Z
`item.finish.first()` returns NULL.
Luke Pearce
CC BY-SA 3.0
2015-12-10T15:18:40.443Z
If I dump `item` I can see the following : `["samplesOrder"]=> array(1) { ["new1"]=> array(3) { ["type"]=> string(5) "order" ["enabled"]=> string(1) "1" ["fields"]=> array(3) { ["appearance"]=> string(7) "SAN0004" ["finish"]=> string(3) "514" ["quantity"]=> string(1) "1" } }}}`
Luke Pearce
CC BY-SA 3.0
2015-12-10T13:25:41.610Z
2015-12-10T15:00:40.053Z
2015-12-10T15:00:40.053Z
carlcs
<p>I have a front end form that saves an entry with a relation field based on an ID. </p>

<pre><code>&lt;input type="hidden" name="items[{{ index }}][finish]" value="{{ finish.id }}"&gt;
</code></pre>

<p>When the entry is saved this ID saves the field with the correct category relation.</p>

<p>I then have a plugin which triggers on entry save that sends an email and attaches the <code>$entry</code>.</p>

<pre><code>craft()-&gt;on( 'entries.saveEntry', function( Event $event ){
    $entry = $event-&gt;params['entry'];
    if( !$event-&gt;params['isNewEntry'] || $entry-&gt;section-&gt;handle != 'sampleOrders' )
    {
        return;
    }
    //die(Craft::dump($entry-&gt;samplesOrder[0]-&gt;finish-&gt;find()));
    $email = new EmailModel();
    $email-&gt;toEmail = 'luke@ten4design.co.uk';
    $email-&gt;subject = 'Order Received';
    $email-&gt;body = '';
    $email-&gt;htmlBody = craft()-&gt;templates-&gt;render( 'pentagonemailservice/_emails/order', array(
        'entry' =&gt; $entry
    ) );
    try{
        craft()-&gt;email-&gt;sendEmail( $email );
    }
    catch( Exception $e ){
        Craft::log( 'Could not send email.', LogLevel::Error );
    }
} );
</code></pre>

<p>For some reason in my email template I cannot access the finish field with the following code. The other fields print correctly. The finish field is a category relation.</p>

<pre><code>{% for item in entry.samplesOrder %}
    {% set appr = craft.entries.section( 'appearances' ).apprId( item.appearance ).first() %}

    &lt;h3 class="appearance__name h beta"&gt;&lt;a class="a" href="{{ appr.url }}"&gt;{{ appr.apprId }} - {{ appr.displayName }} - {{ appr.pentagonRange[0].title }}&lt;/a&gt;&lt;/h3&gt;

    &lt;h3 class="h h6 caps--spaced m-b--05"&gt;Material&lt;/h3&gt;
    &lt;p class="appearance__material h delta basket-bits"&gt;{{ appr.material }}&lt;/p&gt;

    &lt;h3 class="h h6 caps--spaced m-b--05"&gt;Finish&lt;/h3&gt;
    {#&lt;p class="basket-bits"&gt;{{ finish.title }}&lt;/p&gt;#}

    &lt;h3 class="h h6 caps--spaced m-b--05"&gt;Quantity&lt;/h3&gt;
    &lt;p class="basket-bits"&gt;{{ item.quantity }}&lt;/p&gt;
{% endfor %}
</code></pre>

<p>How do I access the finish field in my email template?</p>

<plugin-development><entries><relations>
Accessing relation data in an on save entry event
# Accessing relation data in an on save entry event

(Asked by: username_0 on 2015-12-10)

I have a front end form that saves an entry with a relation field based on an ID. 


When the entry is saved this ID saves the field with the correct category relation.
I then have a plugin which triggers on entry save that sends an email and attaches the $entry.
craft()->on( 'entries.saveEntry', function( Event $event ){
    $entry = $event->params['entry'];
    if( !$event->params['isNewEntry'] || $entry->section->handle != 'sampleOrders' )
    {
        return;
    }
    //die(Craft::dump($entry->samplesOrder[0]->finish->find()));
    $email = new EmailModel();
    $email->toEmail = 'luke@ten4design.co.uk';
    $email->subject = 'Order Received';
    $email->body = '';
    $email->htmlBody = craft()->templates->render( 'pentagonemailservice/_emails/order', array(
        'entry' => $entry
    ) );
    try{
        craft()->email->sendEmail( $email );
    }
    catch( Exception $e ){
        Craft::log( 'Could not send email.', LogLevel::Error );
    }
} );

For some reason in my email template I cannot access the finish field with the following code. The other fields print correctly. The finish field is a category relation.
{% for item in entry.samplesOrder %}
    {% set appr = craft.entries.section( 'appearances' ).apprId( item.appearance ).first() %}

    {{ appr.apprId }} - {{ appr.displayName }} - {{ appr.pentagonRange[0].title }}

    Material
    {{ appr.material }}

    Finish
    {#{{ finish.title }}#}

    Quantity
    {{ item.quantity }}
{% endfor %}

How do I access the finish field in my email template?

**Tags:** plugin-development, entries, relations


---

## 2 answers

### Answer by username_1 on 2015-12-10. Score: 2 | Accepted answer

Related elements, such as categories, are stored by Craft as arrays, even if there is only a single element to be stored.
In your entry form, you don't appear to be storing an array, but only the id of the selected category:


This seems to be borne out by what you see when you dump the item variable in your email template. item[finish] should be an array, even if only a single category is assigned to it.
Try using the following in your entry form to see if that helps, which should store the category id as an array:


You should then be able to access the correct value for the category title using the code in @username_2 answer.


---

### Answer by username_2 on 2015-12-10. Score: 1

I don't think it has something to do with your event listener, and you just access the related elements not quite right. Try to get them like so:
{% set finish = item.finish.first() %}
{% if finish %}
    Finish
    {{ finish.title }}
{% endif %}

or use a relatedTo query to get the finishes form the field.

> **Comments:**
> **username_0**: Using `{{ entry.finish.first().title }}` gave me:  `Impossible to access an attribute ("title") on a null variable`  Tried using `{{ item.finish }}` and it gives me the following:  `Object of class Craft\ElementCriteriaModel could not be converted to string`  But using `{{ item.finish[0] }}` gives me  `Key "0" in object with ArrayAccess of class "Craft\ElementCriteriaModel" does not exist`  
> **username_2**: Yep, sorry. It's the `item` variable, not `entry`. Updated the answer.  
> **username_0**: `item.finish.first()` returns NULL.  
> **username_0**: If I dump `item` I can see the following : `["samplesOrder"]=> array(1) { ["new1"]=> array(3) { ["type"]=> string(5) "order" ["enabled"]=> string(1) "1" ["fields"]=> array(3) { ["appearance"]=> string(7) "SAN0004" ["finish"]=> string(3) "514" ["quantity"]=> string(1) "1" } }}}`  

---

