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
}
],
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)
#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:
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!
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…
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:
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
}
],
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.