How to get all values of array in GridView

Hi guys, I try to get all values of table, but I will get only one. Following method will give me all ForeignKeys in table. It has been checked using var_dump





    public static function fk_for_adresses() {

        $auswahl = Ekontakt::find()->select('id_person')->all();

        $fk = array();

        $x = 0;

        foreach ($auswahl as $wahl) {

            $fk[$x] = $wahl->id_person;

            $x++;

        }

        return $fk;

    }



Folowing code in GridView will only show up both values per line, instead of one (the first one in line 1,the second one in line 2) according ForeignKeys:





    [

        'attribute' => $wird_dargestellt_fuer,

        'label' => Yii::t('app', 'gehört zu'),

        'format' => 'html',

        'value' => function ($model) {

            $namen = "";

            $fk = EKontakt::fk_for_adresses();

            for ($i = 0; $i < count($fk); $i++) {

                $eKontakte = Person::find()->where(['id' => $fk[$i]])->all();

                foreach ($eKontakte as $kontakte) {

                    $namen .= "<div id=''><ul><li><span class='glyphicon glyphicon-earphone'></span>" . $kontakte->vorname . "," . $kontakte->nachname;

                }

                $namen .= "</div></ul></li>";

 			return $namen; // this will show up  same value per line again

            }

            	return $namen; // this will show up both values per line

        }

    ],



Any ideas,what i do wrong?

Describe me exactly what you want very carefully and I will help you.

Screenshots are preferred.

But for first look you have broken markup

I want to get exactly one value according ForeignKey($fk) per line. Actually, I will get ,either same(s.Attachement) or both(s.Attachement) per line

First of all read this https://github.com/yiisoft/yii2/blob/master/docs/guide/db-active-record.md#querying-data-

Next move all db queries from view to search model.

Your code should be like follows:


[

    'attribute' => $wird_dargestellt_fuer,

    'label' => Yii::t('app', 'gehört zu'),

    'format' => 'html',

    'value' => function ($model) {

        $address_parts = [];

        foreach($model->addresses as $address) {

            $address_parts[] = $address->name;

        }

        return implode(', ', $address_parts);

    }

],



This will not work, 'cause $model has not attributes, I want. I have two models being rendered! But model, I need($model_ekontakt) won’t be rendered at all in dataProvider(see here:here)

Anyway all data should come from search model

If I use ur code,Iwill get error "unkown property Kontakadresse:vornamen.vornamen.vornamen is in table person.

It’s complicated,helping me without knowing database, I know

Mayor problem seems to be,that this thread still is unsolved!!

Maybe, it colud be helpfull showing ERD of tables being involved. What do you think?

If I code like this,I will get doubled values again(see Attachement)





    [

        'attribute' => $wird_dargestellt_fuer,

        'label' => Yii::t('app', 'gehört zu'),

        'format' => 'html',

        'value' => function ($model) {

            $fk = EKontakt::fk_for_adresses();

            $address_parts = [];

            for ($i = 0; $i < count($fk); $i++) {

                $eKontakte = Person::find()->where(['id' => $fk[$i]])->all();

                foreach ($eKontakte as $address) {

                    $address_parts[] = $address->vorname." ".$address->nachname;

                }

            }

            return implode(', ', $address_parts);

        }

    ],



#1 This line executes a db query each time a row of the gridview is rendered. When you have 20 rows in the gridview, then you will execute the query in fk_for_adresses() method 20 times.

#2 If fk_for_adresses() returns an array of 100 foreign keys, then this line will run 100 times to do a db query. When you have 20 rows in the gridview, then the query will be executed 2,000 times.

#3 $namen will have a broken markup. At its best, when $eKontakte has only one entry, it will be:

[html]

<div id="">

<ul>

&lt;li&gt;&lt;span class='glyphicon glyphicon-earphone'&gt;&lt;/span&gt;VorName, NachName


&lt;/div&gt;

</ul>

</li>

[/html]

Note that <div> and <li> have their ending counterparts in the wrong places.

When $eKontakte has more than 2 entries, then the things get much worse, because you are opening <div>, <ul> and <li> inside the loop without closing them.

#4 The function returns inside a loop.

#5 You will never get to this line.

#6 Why don’t you use $model? And what model class this $model represents?

I really don’t understand what you want to do.

Please explain your goal without using programmer’s words. What do you want to show to the users with your gridview?

I want, that name in table person being represented by ForeignKey will be shown up in Gridview. That’s all want I want. As my attachement will show,both names are shown up simultaneously!

There are three table being involved in:

1.Kontaktadressen

2.Person

3.EKontakt

Ekontakt includes ForeignKey(id_person) which refers to Person,which includes names. GridView will show up records of Kontaktadressen, which has ForeignKey for table EKontakt !So, Kontaktadressen has no direct relation to person.That’s my mayor problem!

What are Kontaktadressen, Person and EKontakt in the real world?

Please elaborate them without using programmer’s words. No “table”, no “foreign key” please.

These are models based on database, having created using Gii!

I have no idea how to elaborate this in another words. A model is a model,a table is a spreadsheet

ForeignKey is SQL-slang, being described here

Ah, please be patient. Don’t get excited.

I understand that ‘Person’ model and ‘person’ table represents a certain person with his/her first name and family name, like ‘Bob Dylan’ and ‘Bette Midler’. That’s OK. But I don’t understand what ‘Kontaktadressen’ represents in the real world. What is it? And what ‘EKontakt’ means? Could you explain them?

I just have designed an ERDM(Entity-Relationship-Modell)

of tables(models) being involved. Hope,this will help answering ur questions…

P.S.: ‘vornamen’ in ERDM means first name, ‘nachnamen’ means surname. These attributes should be shown in GridView in first column. All other attributes of Kontaktadresse(‘ort’,‘plz’) will already be shown up in GridView. ‘ort’ means place…

OK.

So, Kontaktadressen has one EKontakt, and EKontakt has one Person. Right?

Then, if you have already established the proper relations among them, you should be able to access forname and nachname very easily like the following:




$model = Kontaktaddressen::findOne($id);

$ekontakt = $model->ekontakt;

$person = $ekontakt->person;

echo $person->vorname . ',' . $person->nachname;



What relations do you have among them?

This will not work.$id is not defined. I use an anonymous function in order to get column!. I already had code,which will work,like this. Problem: I will get values simultaniously(see Attachement)





    [

        'attribute' => $wird_dargestellt_fuer,

        'label' => Yii::t('app', 'gehört zu'),

        'format' => 'html',

        'value' => function ($model) {

            $namen = "";

            $fk = EKontakt::fk_for_adresses();

            for ($i = 0; $i < count($fk); $i++) {

                $eKontakte = Person::find()->where(['id' => $fk[$i]])->all();

                foreach ($eKontakte as $kontakte) {

                    $namen .= "<div id=''><ul><li><span class='glyphicon glyphicon-user'></span>" . $kontakte->vorname . "," . $kontakte->nachname;

                }

            }

            $namen .= "</div></ul></li>";

            return $namen; // this will show up both values per line

        }

    ],



There should be ‘ekontakt’ relation in Kontaktadressen model.

You seem to fail to understand the relational active record, one of the very basics of yii framework.

BINGO!!!

U rescued me one more time! Obviously. I thought much to complicated!!

Following code will give me result, as I wanted it for days…





    [

        'attribute' => $wird_dargestellt_fuer,

        'label' => Yii::t('app', 'gehört zu'),

        'format' => 'html',

        'value' => function ($model) {

            $model = frontend\modules\kontakt\models\KontaktAdresse::findOne($model->id);

            $ekontakt = $model->eKontakt;

            $person = $ekontakt->person;

            return $person->vorname . ',' . $person->nachname;

        }

    ],



Output will be shown up in attachement

P.S.: A thousands thanks for ur help. I will precise my question in future without using programmer word.Promised

[color="#FF0000"][size="7"]No![/size][/color]

Programming is not a lottery. :(

The following should be enough:




    [

        'attribute' => $wird_dargestellt_fuer,

        'label' => Yii::t('app', 'gehört zu'),

        'value' => function ($model) {

            return $model->eKontakt->person->vorname . ',' . $model->eKontakt->person->nachname;

        }

    ],



I beg you, please read the guide.

Guide > Working with Databases > Active Record > Working with Relational Data

http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relational-data

BINGO is a german slang for "U got it!" or "Great,I did well". There is no context from BINGO to coincidence in german language

If u asked a german guy about synonym for Bingo,he coud tell U Bingo means…

1.: Great! Super!. I got solution!

2.: a game from England for several old people

BINGO (ビンゴ) is also used in ordinary Japanese, and the meaning is almost the same.

“Oh! I’ve (you’ve) got it at last, by chance”. ;)

We tend to ask useless questions about the present wall in front of us, even when we are at the dead end of a wrong path. Your question, "How to get all values of array in GridView" is an example. In such a situation, we have to come back where we started, instead. We have to review the big map. Where do we want to go? Which is the main road? Where are we now?

It’s very important to tell what is the final goal for the end users in secular language, without using technical jargon. Screenshots and ERs greatly help it in this aspect.

And, you don’t know what yii framework can do for you, yet. It meas that you don’t have a map of yii. There’s no wonder you get lost so often.

Take your time to read through the definitive guide. I promise, it’s really worth doing. 3 days of reading it will save you 300 days in the future. :)