sql - How to bound primary key to custom ON condition in Yii Relations? -


the problem:

how bound primary key custom relation query?

context, for:

one source can relate several different modifications (many_many), each modification relate product (belongs_to). if several products has 1 source, means products same - that's criteria. (i can't merge same products, because may turn out not same, if merge them - can't split them back).

so, when need find orders related product, want find orders same products, not current product.

relation looks this:

'orderedproducts'=>array(self::has_many,'orderproduct','','on'=>('modification_id in ( select distinct ms2.modification_id 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 p1.product_id='.$this->primarykey.' )')),  'orders'=>array(self::has_many,'order',array('order_id'=>'order_id'),'through'=>'orderedproducts'), 

$this->primarykey not working, it's here show need bound primary key.

any suggestions how bound primary key there?

it rather complicated query, , convenient make functionality defining getter getorderedproducts() model. inside getter, can primary key of model $this->primarykey. able ordered products model attribute $model->orderedproject, if you'd made using relations. and, bonus, can implement caching such heavy query.

as relation, research performed logging queries sql constructed when 1 accesses has_many relation field of model shows yii bind primary key of model 1 of named param :ypl0, :ypl1... etc. so, if feel okay dirty hack, may access primary key of model bound param:

'orderedproducts'=>array(self::has_many,'orderproduct','','on'=>('modification_id in ( select distinct ms2.modification_id 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 p1.product_id=:ypl0' 

but, should make sure yii bind primary key first (:ypl0) of params (that true, if not bind params yourself). anyways, rather recommend rely on first approach defining getter more stable , customizable.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -