Answers.Body: <p>A very similar question has been asked before <a href="https://craftcms.stackexchange.com/questions/2316/how-can-i-follow-relations-in-elementcriteriamodels-from-plugin-code">How can I follow relations in ElementCriteriaModels from plugin code</a>. You would want something like this:</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  $criteria-&gt;section = 'products';
  $criteria-&gt;relatedTo = array(
      'targetElement' =&gt; $e,
      'field'         =&gt; $field
  );
  $elements = craft()-&gt;elements-&gt;findElements($criteria);
  </code></pre>
  
  <p>Where <code>$e</code> is testVariant, and <code>$field</code> is <code>productVariants.productSizes</code>, which can be retrieved using <code>craft()-&gt;elements-&gt;getElementById(id)</code> and <code>craft()-&gt;fields-&gt;getFieldById(id)</code> or <code>getFieldByHandle(handle)</code> respectively.</p>
  
  <p>To create a controller see the <a href="http://buildwithcraft.com/docs/plugins/controllers" rel="nofollow noreferrer">documentation on Controllers</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-14T19:41:30.387Z
Answers.LastActivityDate: 2014-10-14T19:41:30.387Z
Answers.LastEditDate: 2017-04-13T12:51:00.200Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>Is there a direct relation between code in templates and PHP code inside a controller for example?
  I need to translate </p>
  
  <pre><code>{% set relatedProducts = craft.entries.section('products').relatedTo({
                     targetElement: testVariant,
                     field: "productVariants.productSizes"
  }) %} 
  </code></pre>
  
  <p>into a controller and im not sure how.</p>
  
Tags: <templating><controller>
Title: Direct relation between templates and PHP
ThreadText: # Direct relation between templates and PHP
  
  (Asked by: username_0 on 2014-10-14)
  
  Is there a direct relation between code in templates and PHP code inside a controller for example?
  I need to translate
  
  ```
  {% set relatedProducts = craft.entries.section('products').relatedTo({
                     targetElement: testVariant,
                     field: "productVariants.productSizes"
  }) %} 
  
  ```
  
  into a controller and im not sure how.
  
  **Tags:** templating, controller
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-10-14. Score: 1
  
  A very similar question has been asked before [How can I follow relations in ElementCriteriaModels from plugin code](https://craftcms.stackexchange.com/questions/2316/how-can-i-follow-relations-in-elementcriteriamodels-from-plugin-code). You would want something like this:
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::Entry);
  $criteria->section = 'products';
  $criteria->relatedTo = array(
      'targetElement' => $e,
      'field'         => $field
  );
  $elements = craft()->elements->findElements($criteria);
  
  ```
  
  Where `$e` is testVariant, and `$field` is `productVariants.productSizes`, which can be retrieved using `craft()->elements->getElementById(id)` and `craft()->fields->getFieldById(id)` or `getFieldByHandle(handle)` respectively.
  
  To create a controller see the [documentation on Controllers](http://buildwithcraft.com/docs/plugins/controllers).
  
  
  ---
  
  