Yii Framework Forum: How to get all values of array in GridView - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to get all values of array in GridView [solved]

#1 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 232
  • Joined: 20-April 17
  • Location:Hannover

Posted 02 November 2017 - 06:28 AM

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?
0

#2 User is offline   umneeq 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 149
  • Joined: 10-October 11
  • Location:Russia

Posted 02 November 2017 - 06:41 AM

Describe me exactly what you want very carefully and I will help you.
Screenshots are preferred.
But for first look you have broken markup
0

#3 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 232
  • Joined: 20-April 17
  • Location:Hannover

Posted 02 November 2017 - 06:51 AM

View Postumneeq, on 02 November 2017 - 06:41 AM, said:

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

Attached File(s)


0

#4 User is offline   umneeq 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 149
  • Joined: 10-October 11
  • Location:Russia

Posted 02 November 2017 - 07:42 AM

First of all read this https://github.com/y...#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);
    }
],

0

#5 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 232
  • Joined: 20-April 17
  • Location:Hannover

Posted 02 November 2017 - 07:57 AM

View Postumneeq, on 02 November 2017 - 07:42 AM, said:

First of all read this https://github.com/y...#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)
0

#6 User is offline   umneeq 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 149
  • Joined: 10-October 11
  • Location:Russia

Posted 02 November 2017 - 08:02 AM

Anyway all data should come from search model
0

#7 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 232
  • Joined: 20-April 17
  • Location:Hannover

Posted 02 November 2017 - 08:08 AM

View Postumneeq, on 02 November 2017 - 08:02 AM, said:

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 knowPosted Image

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);
        }
    ],

Attached File(s)


0

#8 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,375
  • Joined: 16-February 11
  • Location:Japan

Posted 02 November 2017 - 08:56 AM

Quote

        'value' => function ($model) {
            $namen = "";
            $fk = EKontakt::fk_for_adresses(); // #1
            for ($i = 0; $i < count($fk); $i++) {
                $eKontakte = Person::find()->where(['id' => $fk[$i]])->all();  // #2
                foreach ($eKontakte as $kontakte) {
                    $namen .= "<div id=''><ul><li><span class='glyphicon glyphicon-earphone'></span>" . $kontakte->vorname . "," . $kontakte->nachname;
                }
                $namen .= "</div></ul></li>";  // #3
                return $namen; // #4
            }
            return $namen; // #5
            // #6
        }



#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:
<div id="">
  <ul>
    <li><span class='glyphicon glyphicon-earphone'></span>VorName, NachName
    </div>
  </ul>
</li>

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?

Quote

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

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?
0

#9 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 232
  • Joined: 20-April 17
  • Location:Hannover

Posted 02 November 2017 - 09:01 AM

View Postsoftark, on 02 November 2017 - 08:56 AM, said:

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!
0

#10 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,375
  • Joined: 16-February 11
  • Location:Japan

Posted 02 November 2017 - 09:16 AM

What are Kontaktadressen, Person and EKontakt in the real world?
Please elaborate them without using programmer's words. No "table", no "foreign key" please.
0

#11 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 232
  • Joined: 20-April 17
  • Location:Hannover

Posted 02 November 2017 - 09:18 AM

View Postsoftark, on 02 November 2017 - 09:16 AM, said:

What are Kontaktadressen, Person and EKontakt?
Please elaborate them without using programmer's words No "table", "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
0

#12 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,375
  • Joined: 16-February 11
  • Location:Japan

Posted 02 November 2017 - 09:31 AM

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?
1

#13 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 232
  • Joined: 20-April 17
  • Location:Hannover

Posted 02 November 2017 - 09:34 AM

View Postsoftark, on 02 November 2017 - 09:31 AM, said:

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....

Attached File(s)


0

#14 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,375
  • Joined: 16-February 11
  • Location:Japan

Posted 02 November 2017 - 09:51 AM

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?
1

#15 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 232
  • Joined: 20-April 17
  • Location:Hannover

Posted 02 November 2017 - 09:59 AM

View Postsoftark, on 02 November 2017 - 09:51 AM, said:

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
        }
    ],

0

#16 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,375
  • Joined: 16-February 11
  • Location:Japan

Posted 02 November 2017 - 10:05 AM

View Posttklustig, on 02 November 2017 - 09:59 AM, said:

This will not work(there is no attribute ekontakt in any table.ekontakt is a table).

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.
0

#17 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 232
  • Joined: 20-April 17
  • Location:Hannover

Posted 02 November 2017 - 10:08 AM

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.PromisedPosted Image

Attached File(s)


0

#18 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,375
  • Joined: 16-February 11
  • Location:Japan

Posted 02 November 2017 - 10:29 AM

View Posttklustig, on 02 November 2017 - 10:08 AM, said:

BINGO!!!

No!
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.yiiframew...relational-data
0

#19 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 232
  • Joined: 20-April 17
  • Location:Hannover

Posted 02 November 2017 - 12:04 PM

View Postsoftark, on 02 November 2017 - 10:29 AM, said:

No!
Programming is not a lottery. :(


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 Posted Image
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
0

#20 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,375
  • Joined: 16-February 11
  • Location:Japan

Posted 02 November 2017 - 06:47 PM

View Posttklustig, on 02 November 2017 - 12:04 PM, said:

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 Posted Image
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". ;)

Quote

I will precise my question in future without using programmer word.

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. :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users