activerecord - How do I use join and indexBy in Yii2? -
i have this, loads each , every ebay
row individually, generating thousands of sql statements:
$products = \app\models\product::find() ->joinwith('ebay', false, 'inner join') ->indexby(function($row){return $row->ebay->epid;}) ->all();
i tried this, gave error: 'getting unknown property: app\models\product::ebay.epid'
$products = \app\models\product::find() ->joinwith('ebay', false, 'inner join') ->indexby('ebay.epid') ->all();
setting eager loading = true
doesn't either. still loads each row individually loads them again @ end.
how can efficiently join table in yii , index value in joined table?
you won't able indexby
. however, arrayhelper::index
can index array on related model field. here's how can done:
$products = \app\models\product::find() ->with('ebay') ->all(); arrayhelper::index($products, 'ebay.epid');
the code run 2 queries, 1 products, 1 related ebay products. array indexed no db queries @ all.
Comments
Post a Comment