<p>It is indeed confusing behaviour... The reason why it's happening, along with a handful of solutions, is outlined in the answers to this question: <a href="https://craftcms.stackexchange.com/questions/35704/offset-setting-a-limit/35705#35705">Offset setting a limit?</a></p>
<p>As I mentioned in my answer to that question, if possible it would be best to avoid multiple roundtrips to the database, fetching your data once only and then using filters to filter down the full result set later, but in some cases that might not be easy or possible... in which case <code>clone()</code> is a solution, or nullifying the parameters that you set earlier, as per Josh's answer.</p>

CC BY-SA 4.0
2024-03-15T02:28:46.693Z
Hi @JamesSmith using ```clone()``` is work with my code, thank for your answer. Actually i never checking documentation about Upgrading from Craft 2 since i have never use Craft 2 from the start i use Craft.
Nixxian365
CC BY-SA 4.0
2024-03-15T02:32:16.117Z
"if possible it would be best to avoid multiple roundtrips to the database, fetching your data once only and then using filters to filter down the full result set later"
When you mention this, does that mean i need to create a variable ```entry.mediaContentComponent.all()``` in **base.twig**, and than pass it to **content.twig**?
Nixxian365
CC BY-SA 4.0
2024-03-15T09:35:55.407Z
Yes that's right. Depending on how your templates are organised it might not be easy, however, so you'd need to make a judgment call on whether the optimisation is worth it. If content.twig `extends` base.twig, you wouldn't need to explicitly pass a variable - it'll automatically be able to access it through inheritance.
James Smith
CC BY-SA 4.0
2024-03-14T11:03:03.727Z
2024-03-14T11:03:03.727Z
James Smith
<p>i try to get blockMatrix value with different way(<code>.one()</code> &amp; <code>.all()</code>) in the same page (this page consists of several templates) but the result is weird. Here how i do it:</p>
<p>i have a blockMatrix(&quot;<em>mediaContentComponent</em>&quot;) with 2 types:</p>
<ol>
<li><code>textContentComponent</code> =&gt; only plain text field</li>
<li><code>imageContentComponent</code> =&gt; only asset field</li>
</ol>
<p>At first value, i try to get only the first <code>textContentComponent</code> value in blockMatrix with <code>entry.mediaContentComponent.type('textContentComponent').one()</code>, This query will run in <strong>base.twig</strong>.</p>
<p>then on second value, i try to get all the blockMatrix values with <code>entry.mediaContentComponent.all()</code>, this query will run in <strong>content.twig</strong>.</p>
<p>Now after all query is running here is the weird result, the first value didn't have any problem but the second value didn't have any <code>imageContentComponent</code> and only have <code>textContentComponent</code>. I am curious, is this expected behaviour that Craft have?</p>
<p>Note:</p>
<ol>
<li>the blockMatrix value is added randomly by editor. And here is how i handle the second value:</li>
</ol>
<pre><code>{% set contents = entry.mediaContentComponent.all() %}

{% for content in contents %}
   {% switch content.type %}
      {% case 'imageContentComponent' %}
    
      {% case 'textContentComponent' %}

    {% endswitch %}
{% endfor %}

</code></pre>

<craft3><templating>
Can't use matrix.one() and matrix.all() at the same page
# Can't use matrix.one() and matrix.all() at the same page

(Asked by: username_0 on 2024-03-14)

i try to get blockMatrix value with different way(.one() & .all()) in the same page (this page consists of several templates) but the result is weird. Here how i do it:
i have a blockMatrix("mediaContentComponent") with 2 types:

textContentComponent => only plain text field
imageContentComponent => only asset field

At first value, i try to get only the first textContentComponent value in blockMatrix with entry.mediaContentComponent.type('textContentComponent').one(), This query will run in base.twig.
then on second value, i try to get all the blockMatrix values with entry.mediaContentComponent.all(), this query will run in content.twig.
Now after all query is running here is the weird result, the first value didn't have any problem but the second value didn't have any imageContentComponent and only have textContentComponent. I am curious, is this expected behaviour that Craft have?
Note:

the blockMatrix value is added randomly by editor. And here is how i handle the second value:

{% set contents = entry.mediaContentComponent.all() %}

{% for content in contents %}
   {% switch content.type %}
      {% case 'imageContentComponent' %}
    
      {% case 'textContentComponent' %}

    {% endswitch %}
{% endfor %}

**Tags:** craft3, templating

---

## 1 answers

### Answer by username_1 on 2024-03-14. Score: 2 | Accepted answer

It is indeed confusing behaviour... The reason why it's happening, along with a handful of solutions, is outlined in the answers to this question: [Offset setting a limit?](https://craftcms.stackexchange.com/questions/35704/offset-setting-a-limit/35705#35705)

As I mentioned in my answer to that question, if possible it would be best to avoid multiple roundtrips to the database, fetching your data once only and then using filters to filter down the full result set later, but in some cases that might not be easy or possible... in which case `clone()` is a solution, or nullifying the parameters that you set earlier, as per Josh's answer.

> **Comments:**
> **username_0**: Hi @JamesSmith using ```clone()``` is work with my code, thank for your answer. Actually i never checking documentation about Upgrading from Craft 2 since i have never use Craft 2 from the start i use Craft.  
> **username_0**: "if possible it would be best to avoid multiple roundtrips to the database, fetching your data once only and then using filters to filter down the full result set later" When you mention this, does that mean i need to create a variable ```entry.mediaContentComponent.all()``` in **base.twig**, and than pass it to **content.twig**?  
> **username_1**: Yes that's right. Depending on how your templates are organised it might not be easy, however, so you'd need to make a judgment call on whether the optimisation is worth it. If content.twig `extends` base.twig, you wouldn't need to explicitly pass a variable - it'll automatically be able to access it through inheritance.  

---

