<p>I might be misunderstanding but if your <code>my_categories_field</code> is a <em>Categories</em> field then dump <code>{{ dump(entry.my_categories_field) }}</code> and you'll see that the ids aren't there.</p>
<p>Relations are stored in the <code>relations</code> table and are fetched using <code>sourceId</code> and <code>fieldId</code>.</p>
<p>From what I can see, you are on an entry page so what you could do if you wanted to lighten things up a little is to eager-load your categories:</p>
<pre class="lang-twig prettyprint-override"><code>{% do craft.app.elements.eagerLoadElements(
    className(entry),
    [entry],
    ['my_categories_field']
) %}
</code></pre>
<p>Then fetch your entries with:</p>
<pre class="lang-twig prettyprint-override"><code>{% set entriesFeed = craft.entries()
    .relatedTo(entry.my_categories_field)
    .all() %}
</code></pre>
<p>I'm not sure that's <em>the</em> answer but hopefully it helps :)</p>

CC BY-SA 4.0
2021-08-17T08:04:39.897Z
Awesome answer! So my assumption about how the categories field stores its data was indeed wrong, good to know. I'll take a look at the `relations` table to see how it works under the hood, definitely useful to know. Thanks for explaining!
MoritzLost
CC BY-SA 4.0
2021-08-17T07:19:02.297Z
2021-08-17T07:19:02.297Z
Oli
<p>Is there a way to get raw/unformatted values for a field before they're passed to the field's <code>normalizeValue</code> function? That is, get the value close to how it's stored in the database? I haven't found a documented method for this in the documentation.</p>
<h2>Background</h2>
<p>I'm building a section with a categories field, the selected categories act as filters for an entries feed that is displayed on the frontend for that entry. So I'm doing something like this:</p>
<pre><code>{% set selectedCategories = entry.my_categories_field.ids() %}
{% set entriesFeed = craft.entries().relatedTo(selectedCategories).all() %}
</code></pre>
<p>But the additional database query is kind of pointless. As far as I know, the categories field already stores the selected categories as a list of IDs – correct me if I'm wrong. So when the field is accessed, it returns a <code>CategoryQuery</code> populated with a filter to match those IDs. Then I execute that query with <code>.ids()</code> just to get the IDs from the database that are already stored in the field in the first place.</p>
<p>Is there a simple way to get the stored IDs for that field directly without an additional database query? I.e., the value before its normalized to a CategoryQuery by <code>normalizeValue</code>?</p>

<relations><field><query><elementquery>
Get raw/unformatted field values?
# Get raw/unformatted field values?

(Asked by: username_0 on 2021-08-16)

Is there a way to get raw/unformatted values for a field before they're passed to the field's normalizeValue function? That is, get the value close to how it's stored in the database? I haven't found a documented method for this in the documentation.
Background
I'm building a section with a categories field, the selected categories act as filters for an entries feed that is displayed on the frontend for that entry. So I'm doing something like this:
{% set selectedCategories = entry.my_categories_field.ids() %}
{% set entriesFeed = craft.entries().relatedTo(selectedCategories).all() %}

But the additional database query is kind of pointless. As far as I know, the categories field already stores the selected categories as a list of IDs – correct me if I'm wrong. So when the field is accessed, it returns a CategoryQuery populated with a filter to match those IDs. Then I execute that query with .ids() just to get the IDs from the database that are already stored in the field in the first place.
Is there a simple way to get the stored IDs for that field directly without an additional database query? I.e., the value before its normalized to a CategoryQuery by normalizeValue?

**Tags:** relations, field, query, elementquery

---

## 1 answers

### Answer by username_1 on 2021-08-17. Score: 2 | Accepted answer

I might be misunderstanding but if your my_categories_field is a Categories field then dump {{ dump(entry.my_categories_field) }} and you'll see that the ids aren't there.
Relations are stored in the relations table and are fetched using sourceId and fieldId.
From what I can see, you are on an entry page so what you could do if you wanted to lighten things up a little is to eager-load your categories:
{% do craft.app.elements.eagerLoadElements(
    className(entry),
    [entry],
    ['my_categories_field']
) %}

Then fetch your entries with:
{% set entriesFeed = craft.entries()
    .relatedTo(entry.my_categories_field)
    .all() %}

I'm not sure that's the answer but hopefully it helps :)

> **Comments:**
> **username_0**: Awesome answer! So my assumption about how the categories field stores its data was indeed wrong, good to know. I'll take a look at the `relations` table to see how it works under the hood, definitely useful to know. Thanks for explaining!  

---

