Yii2 - trasitive dependency in active records
<p>I have 3 tables.</p>

<p><code>Product [ProductId, ProductName], 
VendorProduct[VPid, ProductId, VPprice], 
Deals[DealId, VPid, DealPrice]</code></p>

<p>I want to show the Deals using active records in GridView. I defined the relations but unable to access the ProductName from table Product.</p>

<p><code>Deals.*, VendorProduct.VPid, Product.ProductName</code></p>

<p>This is the result I want.
My DealsSearch model is like.</p>

<pre><code>$query = Deals::find()-&gt;joinWith(['vendorProduct', 'vendorProduct.product']);
</code></pre>

<p>And I pass that <code>$query</code> to <code>ActiveDataProvider</code> like this.</p>

<pre><code>$dataProvider = new ActiveDataProvider([
   'query' =&gt; $query,
]);
</code></pre>

<p>My GridView Code :</p>

<pre><code>GridView::widget([
    'dataProvider' =&gt; $dataProvider,
    'filterModel' =&gt; $searchModel,
    'columns' =&gt; [
        ['class' =&gt; 'yii\grid\SerialColumn'],
        'DealId',
        'VPid',
        'DealPrice',
        [
          'label' =&gt; 'Product Name',
          'attribute' =&gt; 'vendorProducts.product.ProductName',
        ],
    ],
]);
</code></pre>

<p>It is simple with the normal join query, but I need to do this with <code>ActiveRecord</code> class.</p>

<p>Edit:</p>

<p>In short, I want result of the following query with ActiveRecord.</p>

<pre><code>SELECT Deals.*, VendorProduct.VPid, Product.ProductName
FROM Deals
JOIN VendorProduct ON VendorProduct.VPid = Deals.VPid
JOIN Product ON Product.ProductId = VendorProduct.ProductId;
</code></pre>

<p>I got following array when I print the <code>$model-&gt;vendorProduct</code> in my view.
(I removed extra columns for the sake of readability).</p>

<pre><code>backend\models\VendorProducts Object
(
    [_attributes:yii\db\BaseActiveRecord:private] =&gt; Array
        (
            [VPid] =&gt; 3
            [ProductId] =&gt; 4175
            [VPprice] =&gt; 0
        )
    [_related:yii\db\BaseActiveRecord:private] =&gt; Array
        (
            [product] =&gt; backend\models\Product Object
                (
                    [_attributes:yii\db\BaseActiveRecord:private] =&gt; Array
                        (
                            [ProductId] =&gt; 4175
                            [ProductName] =&gt; Human Resource Consultancy
                        )

                )

        )

    [_errors:yii\base\Model:private] =&gt; 
    [_validators:yii\base\Model:private] =&gt; 
    [_scenario:yii\base\Model:private] =&gt; default
    [_events:yii\base\Component:private] =&gt; Array
        (
        )

    [_behaviors:yii\base\Component:private] =&gt; Array
        (
        )

)
</code></pre>