0

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

Mohan Prasad
  • 682
  • 1
  • 9
  • 34
  • What you mean by "not displaying accordingly"? Wrong data? Empty? Charset? – Clyff Aug 02 '16 at 15:27
  • I have two internal_code fireld in product lines table... that has two foreign keys.. product_id and area_id... for example I can have product_id as 1 for 5 different columns in productlines table.. These 5 different columns with same product_id will have different area_id. Now when I display the try to get the internal_code it conflicts with area_id and product-id and jumble up the internal code. – Mohan Prasad Aug 02 '16 at 15:49

1 Answers1

0

The problem might be the "return" statement being called inside the foreach loop. Gridview column value takes one return. Try imploding the values inside the loop or append as a string and return the imploded string after the loop. That might be the problem.

Jaison Mathew
  • 33
  • 2
  • 9