Direct relation between templates and PHP 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.
A very similar question has been asked before How can I follow relations in ElementCriteriaModels from plugin code. You would want something like this:

$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);


Where $e is testVariant, and $field is productVariants.productSizes, which can be retrieved using craft()-&gt;elements-&gt;getElementById(id) and craft()-&gt;fields-&gt;getFieldById(id) or getFieldByHandle(handle) respectively.

To create a controller see the documentation on Controllers.