I have issues to get a field from a table using two foreign keys.
Explained the relations of the use tables below.
- User table hasMany Companies(company_id).
- Comapny table has company_id.
- Facilities hasOne Company_id(Based on the user instance).
- Area hasMany Facilites(facility_id).
The logged on user will have areas related to facilities_id which in turn is related to company_id.
I now have a
- Productlines hasMany Products(product_id).
The user should display products related to the areas.
- Productlines hasMany Areas(area_id).
- Product lines has a field called internal_code.
Product model has relation with productlines on the basis of products_id.
I want to display this internal code on the basis of product_id which belongs to the specific areas.
My code as of now which is not working :
In my product Model.
public function getFacility()
{
return $this->hasMany(Facility::className(),['facility_id' => 'facility_id'])->viaTable('sim_users',['company_id'=> Yii::$app->user->identity->company_id]);
}
public function getArea()
{
return $this->hasMany(Area::className(),['area_id'=>'area_id'])->viaTable('sim_facility',['facility_id'=> 'facility_id' ]);
}
public function getProductlines()
{
return $this->hasMany(Productlines::className(), ['product_id' => 'product_id'])->viaTable('sim_productlines',['product_id' => 'product_id']);
}
In my view file:
[
'label' => 'Internal Code',
'format' => 'raw',
'value' => function ($data) {
foreach ($data->productlines as $intCode)
return $intCode->internal_code;
}
I don't understand how to link these relations. Looking for help. Thanks
List of Tables:
- Users (primaryKey - users_id, foreign_key- comapny_id)
- Company(primaryKey - company_id)
- Facility(primaryKey - facility_id, foreignKey- company_id)
- Areas(primaryKey - area_id, foreignKey- facility_id)
- Productlines(primaryKey - productlines_id, foreignKey - product_id and area_id)
- Product (primaryKey - product_id).
My expected result:
In the above table you can see area_id 47 has two product_id's (1 and 3). and the internal code for them is different. I want them to return both the internal codes as of now I can get only one internal code.