title: How to bound primary key to custom ON condition in Yii Relations?
body: <p><strong>The problem:</strong></p>
  
  <p>How to bound primary key to custom relation query? </p>
  
  <p><strong>Context, what for:</strong></p>
  
  <p>One Source can relate to several different Modifications (MANY_MANY),  each modification relate to some Product (BELONGS_TO).
  If several Products has one Source, it means that the Products are same - that's criteria. (I can't just merge same products, because it may turn out that they are not the same, but if I merge them - I can't split them back).</p>
  
  <p>So, when I need to find all orders related to some product, I actually want to find all orders with same products, not only with current product. </p>
  
  <p><strong>Relation looks like this:</strong></p>
  
  <pre><code>'orderedProducts'=&gt;array(self::HAS_MANY,'OrderProduct','','on'=&gt;('modification_id IN (
  select DISTINCT ms2.modification_id FROM products p1
  LEFT JOIN products_modifications pm ON pm.product_id = p1.product_id
  LEFT JOIN modifications_sources ms ON ms.modification_id = pm.modification_id
  LEFT JOIN modifications_sources ms2 ON ms2.source_id = ms.source_id
  where p1.product_id='.$this-&gt;primaryKey.'
  )')),
  
  'orders'=&gt;array(self::HAS_MANY,'Order',array('order_id'=&gt;'order_id'),'through'=&gt;'orderedProducts'),
  </code></pre>
  
  <p>$this->primaryKey is not working, It's here just to show where I need to bound primary key.</p>
  
  <p>Any suggestions how to bound primary key there?</p>