Answers.Body: <p>You're not misunderstanding anything, and your concerns are perfectly valid. I feel like many developers using Craft do not realise that <code>.one()</code> and <code>.all()</code> are in fact database calls, and pretty expensive ones at that. Do it your way, watch the number of DB queries go down in the debug toolbar and feel all warm and fuzzy. (Also, you don't need parentheses in conditionals unless you're doing some complex grouping - <code>{% if prodDim and prodSpc %}</code>).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-10T19:10:14.160Z
Answers.Text: Also worth looking at is [Eager loading](https://craftcms.com/docs/3.x/dev/eager-loading-elements.html#eager-loading-image-transform-indexes).
Answers.UserDisplayName: RitterKnight
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-10T06:06:24.453Z
Answers.LastActivityDate: 2020-08-10T06:06:24.453Z
Answers.UserDisplayName: James Smith
Body: <p>I’ve been wrestling with a site from another developer (who will not be named). He seems to have a pretty solid grasp of PHP, and I had assumed that he also understood Craft well.</p>
  <p>However, as I started to look at his templates, I came across code like this:</p>
  <pre><code>{% if
    (entry.productDimensionsImage.one() or
     entry.productSpecifications.one())
  %}
  </code></pre>
  <p>I would have written it using count() instead of one(), or if I really needed the entry:</p>
  <pre><code>{% set prodDim = entry.productDimensionsImage.one() %}
  {% set prodSpc = entry.productSpecifications.one() %}
  {% if (prodDim and prodSpc) %}
  </code></pre>
  <p>My main concern is that the exact same queries are being used repeatedly throughout a single template; he’s going back to the database over and over again to check the same entry. Then, after he’s done so half a dozen times, then he finally sets it in a variable to loop through it.</p>
  <p>So, is there something I’ve misunderstood? Is there a good reason to keep querying the database multiple times in a single template file?</p>
  <p>TIA</p>
  
Tags: <database><queries>
Title: Is this a basic error, or do I misunderstand how Craft queries the database?
ThreadText: # Is this a basic error, or do I misunderstand how Craft queries the database?
  
  (Asked by: username_0 on 2020-08-09)
  
  I’ve been wrestling with a site from another developer (who will not be named). He seems to have a pretty solid grasp of PHP, and I had assumed that he also understood Craft well.
  
  However, as I started to look at his templates, I came across code like this:
  
  ```
  {% if
    (entry.productDimensionsImage.one() or
     entry.productSpecifications.one())
  %}
  
  ```
  
  I would have written it using count() instead of one(), or if I really needed the entry:
  
  ```
  {% set prodDim = entry.productDimensionsImage.one() %}
  {% set prodSpc = entry.productSpecifications.one() %}
  {% if (prodDim and prodSpc) %}
  
  ```
  
  My main concern is that the exact same queries are being used repeatedly throughout a single template; he’s going back to the database over and over again to check the same entry. Then, after he’s done so half a dozen times, then he finally sets it in a variable to loop through it.
  
  So, is there something I’ve misunderstood? Is there a good reason to keep querying the database multiple times in a single template file?
  
  TIA
  
  **Tags:** database, queries
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-08-10. Score: 3 | Accepted answer
  
  You're not misunderstanding anything, and your concerns are perfectly valid. I feel like many developers using Craft do not realise that `.one()` and `.all()` are in fact database calls, and pretty expensive ones at that. Do it your way, watch the number of DB queries go down in the debug toolbar and feel all warm and fuzzy. (Also, you don't need parentheses in conditionals unless you're doing some complex grouping - `{% if prodDim and prodSpc %}`).
  
  > **Comments:**
  > **username_2**: Also worth looking at is [Eager loading](https://craftcms.com/docs/3.x/dev/eager-loading-elements.html#eager-loading-image-transform-indexes).  
  
  ---
  
  