Answers.Body: <p>Something like this should work to combine <code>'and'</code> and <code>'or'</code> conditions.</p>
  
  <pre class="lang-php prettyprint-override"><code>'relatedTo' =&gt; [
      'and',
      ['element' =&gt; [124, 99]],
      ['element' =&gt; 105],
      ['element' =&gt; 231],
  ],
  </code></pre>
  
  <p>And this is the pattern I’m usually using to build the <code>relatedTo</code> param dynamically. I also try to be explicit about the direction (<code>sourceElement</code> / <code>targetElement</code>) and the relation’s field.</p>
  
  <pre class="lang-php prettyprint-override"><code>$query = craft()-&gt;request-&gt;getQuery();
  
  $relatedToParam = ['and'];
  
  if ($query['reg']) {
      $relatedToParam[] = [
          'targetElement' =&gt; $query['reg'],
          'field' =&gt; 'myRegFieldHandle',
      ];
  }
  
  // repeat for all our other relatedTo conditions
  
  $relatedToParam = count($relatedToParam) &gt; 1 ? $relatedToParam : null,
  
  return [
      'criteria' =&gt; [
          'section' =&gt; 'resources',
          'relatedTo' =&gt; $relatedToParam,
      ],
  ];
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-30T16:21:52.260Z
Answers.Text: That did the trick. I really don't know why I've used the field name instead of the 'element'. For the dynamic building of param, how would that fit into the Element API?
Answers.UserDisplayName: Lukigi
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-30T16:24:11.743Z
Answers.Text: You would just use the code above in your endpoint, do the param building before returning the array with the `'criteria'` and everything.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-30T14:43:57.560Z
Answers.LastActivityDate: 2017-01-30T16:20:58.307Z
Answers.LastEditDate: 2017-01-30T16:20:58.307Z
Answers.UserDisplayName: carlcs
Body: <p>I have a bit complex filtering system that I try to replicate using Element API.</p>
  
  <p>So far I have this which works to a degree:</p>
  
  <p><em>Example url: training.json?reg=124&amp;cat=99&amp;prod=129&amp;erp=</em></p>
  
  <pre><code>'criteria' =&gt; [
              'section' =&gt; 'resources',
              'search' =&gt; (craft()-&gt;request-&gt;getParam('q') ? craft()-&gt;request-&gt;getParam('q')."*" : ''),
              'relatedTo' =&gt; [
                  'and', 
                  craft()-&gt;request-&gt;getParam('reg'),
                  (craft()-&gt;request-&gt;getParam('cat') ? craft()-&gt;request-&gt;getParam('cat') : craft()-&gt;request-&gt;getParam('reg')),
                  (craft()-&gt;request-&gt;getParam('prod') ? craft()-&gt;request-&gt;getParam('prod') : craft()-&gt;request-&gt;getParam('reg')),
                  (craft()-&gt;request-&gt;getParam('erp') ? craft()-&gt;request-&gt;getParam('erp') : craft()-&gt;request-&gt;getParam('reg')),
              ],
    ...
  </code></pre>
  
  <p>First of all, I don't know how to deal with optional parameters, I've solved this by re-using the only required parameter - <strong>reg</strong>. It works, but it's not nice, any help here would be appreciated.</p>
  
  <p>The real issue though is that I need the <strong>reg</strong> parameter to be an array with all the elements in the array having OR operator.</p>
  
  <p>So basically:</p>
  
  <p><em>Example url: training.json?reg[]=124&amp;reg=[]=123&amp;cat=99&amp;prod=129&amp;erp=</em></p>
  
  <p>(reg[0] OR reg[1]) AND cat AND prod AND erp</p>
  
  <p>Is there any way I can achieve this in element API? I've tried something in the lines of the following, but with no luck.</p>
  
  <pre><code>'relatedTo' =&gt; [
       'and', 
        ['targetRegions' =&gt; ['or', 124, 99]], 
        105, 
        231,
   ],
  </code></pre>
  
Tags: <relations><plugin-elementapi><relatedto>
Title: Mixed relation criteria operator in Element API
ThreadText: # Mixed relation criteria operator in Element API
  
  (Asked by: username_0 on 2017-01-28)
  
  I have a bit complex filtering system that I try to replicate using Element API.
  
  So far I have this which works to a degree:
  
  *Example url: training.json?reg=124&cat=99&prod=129&erp=*
  
  ```
  'criteria' => [
              'section' => 'resources',
              'search' => (craft()->request->getParam('q') ? craft()->request->getParam('q')."*" : ''),
              'relatedTo' => [
                  'and', 
                  craft()->request->getParam('reg'),
                  (craft()->request->getParam('cat') ? craft()->request->getParam('cat') : craft()->request->getParam('reg')),
                  (craft()->request->getParam('prod') ? craft()->request->getParam('prod') : craft()->request->getParam('reg')),
                  (craft()->request->getParam('erp') ? craft()->request->getParam('erp') : craft()->request->getParam('reg')),
              ],
    ...
  
  ```
  
  First of all, I don't know how to deal with optional parameters, I've solved this by re-using the only required parameter - **reg**. It works, but it's not nice, any help here would be appreciated.
  
  The real issue though is that I need the **reg** parameter to be an array with all the elements in the array having OR operator.
  
  So basically:
  
  *Example url: training.json?reg[]=124&reg=[]=123&cat=99&prod=129&erp=*
  
  (reg[0] OR reg[1]) AND cat AND prod AND erp
  
  Is there any way I can achieve this in element API? I've tried something in the lines of the following, but with no luck.
  
  ```
  'relatedTo' => [
       'and', 
        ['targetRegions' => ['or', 124, 99]], 
        105, 
        231,
   ],
  
  ```
  
  **Tags:** relations, plugin-elementapi, relatedto
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-01-30. Score: 2 | Accepted answer
  
  Something like this should work to combine `'and'` and `'or'` conditions.
  
  ```php
  'relatedTo' => [
      'and',
      ['element' => [124, 99]],
      ['element' => 105],
      ['element' => 231],
  ],
  
  ```
  
  And this is the pattern I’m usually using to build the `relatedTo` param dynamically. I also try to be explicit about the direction (`sourceElement` / `targetElement`) and the relation’s field.
  
  ```php
  $query = craft()->request->getQuery();
  
  $relatedToParam = ['and'];
  
  if ($query['reg']) {
      $relatedToParam[] = [
          'targetElement' => $query['reg'],
          'field' => 'myRegFieldHandle',
      ];
  }
  
  // repeat for all our other relatedTo conditions
  
  $relatedToParam = count($relatedToParam) > 1 ? $relatedToParam : null,
  
  return [
      'criteria' => [
          'section' => 'resources',
          'relatedTo' => $relatedToParam,
      ],
  ];
  
  ```
  
  > **Comments:**
  > **username_0**: That did the trick. I really don't know why I've used the field name instead of the 'element'. For the dynamic building of param, how would that fit into the Element API?  
  > **username_1**: You would just use the code above in your endpoint, do the param building before returning the array with the `'criteria'` and everything.  
  
  ---
  
  