Answers.Body: <p>I did something similar to this in Twig this week. My query ended up looking like this based on the <a href="https://craftcms.com/docs/4.x/relations.html#the-relatedto-parameter" rel="nofollow noreferrer">craft cms docs</a>.</p>
  <pre><code>{% set query = craft.entries()
     .section('news')
     .search('isVisibleInAllLocations::true OR visibleInLocations:locationSlug)
     .status('live')
     .orderBy('postDate DESC')
     .all() 
  %}
  </code></pre>
  <p>Please note I am using the search method for this, allowing me to pass parameters that either contain or match exactly. The <code>::</code> means the field contains the exact value, and <code>:</code> means it contains part of that value. You can also use <code>score</code> as an orderBy in this case due to the usage of the search parameter, but news feeds tend to be most recent first.</p>
  <p>I believe you would transfer the above Twig into the following PHP, please note that I haven't transfer Twig into PHP in a while</p>
  <pre><code>$query = Entry::find()
      -&gt;section('news')
      -&gt;search('isVisibleInAllLocations::true OR visibleInLocations:locationSlug')
      -&gt;status('live')
      -&gt;orderBy('postDate DESC')
      -&gt;all();
  </code></pre>
  <p>I hope you find this helpful! Interested to know if this works for you or if you have an alternate solution.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-11T23:46:10.180Z
Answers.LastActivityDate: 2024-01-11T23:51:26.063Z
Answers.LastEditDate: 2024-01-11T23:51:26.063Z
Answers.UserDisplayName: kelly
Body: <p>I have the following challenge:</p>
  <ul>
  <li>we have a news channel for multiple company locations.</li>
  <li>Some news should be visible globally (for all locations), but some news are only visible for location X.</li>
  </ul>
  <p>But when a visitor visits <strong>location-x</strong>.company.com/news/, both news entries should be visible:</p>
  <ul>
  <li><strong>news that are visible globally</strong></li>
  <li><strong>OR</strong></li>
  <li><strong>news that aren't visible globally, but have specific location X in
  the entries field</strong></li>
  </ul>
  <p>I have two fields in a channel:</p>
  <ul>
  <li>Checkbox &quot;Visible everywhere&quot;? - yes / no</li>
  </ul>
  <p><a href="https://i.sstatic.net/tlQRP.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/tlQRP.png" alt="checkbox" /></a></p>
  <ul>
  <li>If &quot;Visible everyhwere&quot; == false, an Entries Field is shown &quot;Visible in locations: []&quot;</li>
  </ul>
  <p><a href="https://i.sstatic.net/kZnVF.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kZnVF.png" alt="checkbox false, another field shown - entries field" /></a></p>
  <p><strong>How can I generate a query in PHP for that?</strong></p>
  <p>I tried this, but it does not work - orWhere is ignored and it just ends up as &quot;AND&quot; in the SQL query.</p>
  <pre class="lang-php prettyprint-override"><code>$query-&gt;relatedTo(['targetElement' =&gt; $locationId, 'field' =&gt; 'visibleInLocations']);
  
  $query-&gt;orWhere(['isVisibleInAllLocations' =&gt; true]);
  </code></pre>
  <p>The following does not work either (&quot;Column not found: 1054 Unknown column 'relatedTo' in 'where clause'&quot;)</p>
  <pre class="lang-php prettyprint-override"><code>$query-&gt;andWhere([
      'or',
      ['relatedTo' =&gt; ['targetElement' =&gt; $locationId, 'field' =&gt; 'visibleInLocations']],
      ['isVisibleInAllLocations' =&gt; true],
  ]);
  </code></pre>
  <p>I made complex queries before, but not with relatedTo:</p>
  <pre class="lang-php prettyprint-override"><code> $query-&gt;andWhere([
                'or',
                [
                    // for normal entries
                    'and',
                    ['=', $isProjectColumnName, 0],
                    ['&gt;=', $minPriceColumn, $minPrice],
                    ['&lt;=', $maxPriceColumn, $maxPrice],
                ],
                [
                    // for special project entries
                    'and',
                    ['=', $isProjectColumnName, 1],
                    // other fields for comparision
                    ['&gt;=', $minPriceColumnForProjects, $minPrice],
                    ['&lt;=', $minPriceColumnForProjects, $maxPrice],
                ]
            ]);
  </code></pre>
  <p>Has anybody solved this yet?</p>
  <p>Thanks very much in advance for any hints! </p>
  <p>(I searched almost everywhere, only found issues about chaining relatedTo or this quick hint <a href="https://craftcms.stackexchange.com/a/30348">https://craftcms.stackexchange.com/a/30348</a> - but don't know if this would be the easiest approach for my use case)</p>
  
Tags: <database><query><relatedto><sql>
Title: Relations query with OR: Find entries based on (lightswitch = false AND relatedTo()) OR (lightswitch = true) / nested relatedTo?
ThreadText: # Relations query with OR: Find entries based on (lightswitch = false AND relatedTo()) OR (lightswitch = true) / nested relatedTo?
  
  (Asked by: username_0 on 2023-12-22)
  
  I have the following challenge:
  
  * we have a news channel for multiple company locations.
  * Some news should be visible globally (for all locations), but some news are only visible for location X.
  
  But when a visitor visits **location-x**.company.com/news/, both news entries should be visible:
  
  * **news that are visible globally**
  * **OR**
  * **news that aren't visible globally, but have specific location X in
    the entries field**
  
  I have two fields in a channel:
  
  * Checkbox "Visible everywhere"? - yes / no
  
  [![checkbox](https://i.sstatic.net/tlQRP.png)](https://i.sstatic.net/tlQRP.png)
  
  * If "Visible everyhwere" == false, an Entries Field is shown "Visible in locations: []"
  
  [![checkbox false, another field shown - entries field](https://i.sstatic.net/kZnVF.png)](https://i.sstatic.net/kZnVF.png)
  
  **How can I generate a query in PHP for that?**
  
  I tried this, but it does not work - orWhere is ignored and it just ends up as "AND" in the SQL query.
  
  ```php
  $query->relatedTo(['targetElement' => $locationId, 'field' => 'visibleInLocations']);
  
  $query->orWhere(['isVisibleInAllLocations' => true]);
  
  ```
  
  The following does not work either ("Column not found: 1054 Unknown column 'relatedTo' in 'where clause'")
  
  ```php
  $query->andWhere([
      'or',
      ['relatedTo' => ['targetElement' => $locationId, 'field' => 'visibleInLocations']],
      ['isVisibleInAllLocations' => true],
  ]);
  
  ```
  
  I made complex queries before, but not with relatedTo:
  
  ```php
   $query->andWhere([
                'or',
                [
                    // for normal entries
                    'and',
                    ['=', $isProjectColumnName, 0],
                    ['>=', $minPriceColumn, $minPrice],
                    ['<=', $maxPriceColumn, $maxPrice],
                ],
                [
                    // for special project entries
                    'and',
                    ['=', $isProjectColumnName, 1],
                    // other fields for comparision
                    ['>=', $minPriceColumnForProjects, $minPrice],
                    ['<=', $minPriceColumnForProjects, $maxPrice],
                ]
            ]);
  
  ```
  
  Has anybody solved this yet?
  
  Thanks very much in advance for any hints!
  
  (I searched almost everywhere, only found issues about chaining relatedTo or this quick hint <https://craftcms.stackexchange.com/a/30348> - but don't know if this would be the easiest approach for my use case)
  
  **Tags:** database, query, relatedto, sql
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-01-11. Score: 1
  
  I did something similar to this in Twig this week. My query ended up looking like this based on the [craft cms docs](https://craftcms.com/docs/4.x/relations.html#the-relatedto-parameter).
  
  ```
  {% set query = craft.entries()
     .section('news')
     .search('isVisibleInAllLocations::true OR visibleInLocations:locationSlug)
     .status('live')
     .orderBy('postDate DESC')
     .all() 
  %}
  
  ```
  
  Please note I am using the search method for this, allowing me to pass parameters that either contain or match exactly. The `::` means the field contains the exact value, and `:` means it contains part of that value. You can also use `score` as an orderBy in this case due to the usage of the search parameter, but news feeds tend to be most recent first.
  
  I believe you would transfer the above Twig into the following PHP, please note that I haven't transfer Twig into PHP in a while
  
  ```
  $query = Entry::find()
      ->section('news')
      ->search('isVisibleInAllLocations::true OR visibleInLocations:locationSlug')
      ->status('live')
      ->orderBy('postDate DESC')
      ->all();
  
  ```
  
  I hope you find this helpful! Interested to know if this works for you or if you have an alternate solution.
  
  
  ---
  
  