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

2
  • What you mean by "not displaying accordingly"? Wrong data? Empty? Charset? Commented Aug 2, 2016 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. Commented Aug 2, 2016 at 15:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@MohanPrasad please update the question according to your new change.
I have added my changes -@ck_arjun

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.