Answers.Body: <p>You need to pass an array of element IDs, <em>where the first index is <code>"and"</code></em> – just like in your template (Twig) code – to <code>targetElement</code>. Basically, this just means that you need to prepend the string <code>"and"</code> to your array of IDs.</p>
  
  <p>Something like this should do it:</p>
  
  <pre><code>...
  
  $prodCriteria-&gt;relatedTo = array(
      'targetElement' =&gt; array_merge(['and'], $catIds),
  );
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-25T23:18:01.970Z
Answers.Text: Once again, beaten by Mats.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-25T23:21:04.913Z
Answers.Text: Actually, I think _you_ beat _me_ by about 10 seconds. And I edited my answer :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-25T23:01:40.857Z
Answers.LastActivityDate: 2017-09-25T23:01:40.857Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.Body: <p>Completely untested, but I believe what you're looking for is:</p>
  
  <pre><code>array_unshift($catIds , 'and');
  
  $prodCriteria-&gt;relatedTo = array(
      'targetElement' =&gt; $catIds // Where $catIds is an array of ids
  );  
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-25T23:01:24.700Z
Answers.LastActivityDate: 2017-09-25T23:01:24.700Z
Answers.UserDisplayName: Brad Bell
Body: <p>I have an AJAX <code>GET</code> call that should count all the products matching three different taxonomies.</p>
  
  <ul>
  <li>Product families  </li>
  <li>Sectors of application</li>
  <li>Liquids suitable for the product</li>
  </ul>
  
  <p>so in the form the user could select <code>pumps</code>, <code>agriculture</code>, <code>diesel</code> and the controller called should count the products that matches all those criteria.</p>
  
  <p>I get the solution in the templating engine:</p>
  
  <pre><code>{% set relationParams = ['and', id1, id2, id3 ] %}
  {% set products = craft.entries.section('products').relatedTo(relationParams) %}
  </code></pre>
  
  <p>But something is not working as expected in the controller:</p>
  
  <pre><code>        $prodCriteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry)-&gt;section('products');
  
          $prodCriteria-&gt;relatedTo = array(
              'and',
              'targetElement' =&gt; $catIds // Where $catIds is an array of ids
          );            
          $products = $prodCriteria-&gt;count();
  
          $this-&gt;returnJson( $products );    
  </code></pre>
  
  <p>It seems like <code>relatedTo</code> behave like an <code>or</code> operator. 
  Also, is it possible to make the relatedTo behave in a more complex way, like, I want to use <code>or</code> for product families and <code>and</code> for other taxonomies?</p>
  
Tags: <relations><controller>
Title: Query entries relatedTo multiple categories in controller
ThreadText: # Query entries relatedTo multiple categories in controller
  
  (Asked by: username_0 on 2017-09-25)
  
  I have an AJAX `GET` call that should count all the products matching three different taxonomies.
  
  * Product families
  * Sectors of application
  * Liquids suitable for the product
  
  so in the form the user could select `pumps`, `agriculture`, `diesel` and the controller called should count the products that matches all those criteria.
  
  I get the solution in the templating engine:
  
  ```
  {% set relationParams = ['and', id1, id2, id3 ] %}
  {% set products = craft.entries.section('products').relatedTo(relationParams) %}
  
  ```
  
  But something is not working as expected in the controller:
  
  ```
          $prodCriteria = craft()->elements->getCriteria(ElementType::Entry)->section('products');
  
          $prodCriteria->relatedTo = array(
              'and',
              'targetElement' => $catIds // Where $catIds is an array of ids
          );            
          $products = $prodCriteria->count();
  
          $this->returnJson( $products );    
  
  ```
  
  It seems like `relatedTo` behave like an `or` operator.
  Also, is it possible to make the relatedTo behave in a more complex way, like, I want to use `or` for product families and `and` for other taxonomies?
  
  **Tags:** relations, controller
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-09-25. Score: 2 | Accepted answer
  
  You need to pass an array of element IDs, *where the first index is `"and"`* – just like in your template (Twig) code – to `targetElement`. Basically, this just means that you need to prepend the string `"and"` to your array of IDs.
  
  Something like this should do it:
  
  ```
  ...
  
  $prodCriteria->relatedTo = array(
      'targetElement' => array_merge(['and'], $catIds),
  );
  
  ```
  
  > **Comments:**
  > **username_2**: Once again, beaten by username_1.  
  > **username_1**: Actually, I think _you_ beat _me_ by about 10 seconds. And I edited my answer :)  
  
  ---
  
  ### Answer by username_2 on 2017-09-25. Score: 1
  
  Completely untested, but I believe what you're looking for is:
  
  ```
  array_unshift($catIds , 'and');
  
  $prodCriteria->relatedTo = array(
      'targetElement' => $catIds // Where $catIds is an array of ids
  );  
  
  ```
  
  
  ---
  
  