当存在多个关系时,使用via()或viaTable()显示属性-YII2

I have for models say, supplier-machine, supplier, supplier-to-part and parts. enter image description here

This is how these tables are related to each other.

In Supplier model the relation is defined as below to retrieve part_name of Part table keeping supplier-to-part as junction table.

public function getSupplierToParts()
{
    return $this->hasMany(SupplierToPart::className(), ['supplier_id' => 'id']);
}


public function getParts()
{
    return $this->hasMany(Part::className(), ['id' => 'part_id'])->viaTable('supplier_to_part', ['supplier_id' => 'id']);
}

In Detail view I use implode to display part_name

[
    'attribute'=>'Nature of business',
    'value' => implode(\yii\helpers\ArrayHelper::map($model->parts, 'id', 'part_name')),

    ],

My question is how do I display part_name in supplier-machine model instead of supplier model?? In this case I think supplier and supplier-to-part becomes like a two junction tables. How do I solve this?

You can access the parts through the supplier relation via $model->supplier->parts, assuming your relation to Supplier in your SupplierMachine model is supplier. You still have to account for the multiple parts though:

'value' => implode(",", 
    \yii\helpers\ArrayHelper::map($model->supplier->parts, 'id', 'part_name')
),