In my Yii2 I have tables and relationship setup.
List of tables:
- User table -> foreign key is : company_id.
- Facility_table -> foreign key is : company_id.
- Area_table -> foreign key is : facility_id.
- productlinestable -> foreign key is : product_id and area_id
- producttable -> has a differnet foreign key.. not related to this question.
Now I want to display the user with products which belongs to this user. I am able to do that.
Productlines table has a field called internal_code.I want to display this internal code on the basis of area_d in the product index page.
The Issue is when I loop through the data and display the internal code, the first internal_code is taken and for the rest of internal_code the same data is displayed.
My code is below.
in my product.php model
public function getFacilitiesID()
{
$ids = [];
$facilityID = Facility::find()->where(['company_id' => \Yii::$app->user->identity->company_id])->all();
foreach ($facilityID as $facID){
$ids[] = $facID->facility_id;
}
return $ids;
}
public function getAreaID()
{
$ids = [];
$areaID = Area::find()->where(['facility_id' => $this->getFacilitiesID()])->all();
foreach ($areaID as $arID){
$ids[] = $arID->area_id;
}
return $ids;
}
public function getInternalCode()
{
$ids = [];
$internalCode = Productlines::find()->where(['area_id' => $this->getAreaID()])->all();
foreach ($internalCode as $intCode){
$ids[] = $intCode;
}
var_dump($ids); exit();
return $ids;
}
public function listen()
{
$model = $this->getInternalCode();
// var_dump($mod->getIntern $mod->getInternalCode();
$provider = new \yii\data\ArrayDataProvider([
'allModels' => $model,
'pagination' => [
'pageSize' => 10,
],
]);
return $provider->getModels();
}
In the index page grid view my code is:
[
'label' => 'Internal Code',
'format' => 'raw',
'value' => function ($data) {
$img ='';
foreach ($data->listen() as $key){
$img = $img.$key->internal_code;
}
return $img;
}
],
Can anyone findout whats the solution?
When I var_dump the getInternalCode() function it displays as expected but in the grid view its not displaying accordingly.
Thank you