Relations and widgets

Hi all,

I’m new here so hello everybody.

I’m also a newbie with php and Yii2 (I’m trying to learn both of them).

I red a lot of the Yii2 Documentation and other videos and so on…but I still find a bit difficult to make some changes.

I would like to have some answers in order to get a better idea on “how to do stuff” :P


So let’s say I have two tables: User and Email with a 1:N relation.

I used gii to obtain all the MVC files for both. Each class has at least the id PK and some other columns.

In the file view.php (which outputs the details of the User) there is the DetailView::widget.

The problem is that I can’t get Email working with this widget.

Here is a snippet from view.php




$attributes = [

            'idUser',

            'name',

            'surname',

             ...

            [

              'attribute' => 'Email',

              'value' => $model->emails,

            ]      

                   ];

    

    echo DetailView::widget([

        'model' => $model,

        'attributes' => $attributes,

         ]) 

 

In the code above emails is the relation name in the User model.

However I get the error:

So, how to do that?

How to display every Email of the User in the Detail Widget?

Thank you

P.S.

Sorry for the very basic questions. I searched online for a solution, really. But I only got an headache :P

Another little question.

If I work in a separate php bloc I can do:


foreach ($model->emails as $mail) {

                      echo $mail->email ."<br/>";

        }

But If I do


$model->emails->email

i got the error

I understand this errors. Indeed "emails" is a relation and not an object. But then why it works in the foreach with


$mail->email

?

Any hints, please? :)

Just to kick me in and let me enjoy using YII. :)

Something like




$attributes = [

            'idUser',

            'name',

            'surname',

             ...

            [

              'attribute' => 'Email',

              'value' => implode('<br/>', $model->emails),

            ]      

                   ];

    

    echo DetailView::widget([

        'model' => $model,

        'attributes' => $attributes,

         ])



On your second point emails is an array so you would have to do something like




$model->emails[0]->email



Thank you for your response.

Using your snippet I got the error

Is Email an array?

Still struggling with this.

I’ve tried different ways, all wrong.

I got that I’ve to convert my results in string format in order to get DetailView to output them.

I did a vad_dump on $model->email




vardump($model->emails) 

array(2) {

  [0]=>

  object(app\models\Email)#79 (<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> {

    ["_attributes":"yii\db\BaseActiveRecord":private]=>

    array(5) {

      ["idEmail"]=>

      int(1)

      ["email"]=>

      string(26) “mailaddres@gmail.com"

      ["ksUtente"]=>

      int(3)

      ["ksAzienda"]=>

      NULL

      ["last_update"]=>

      NULL

    }

    ["_oldAttributes":"yii\db\BaseActiveRecord":private]=>

    array(5) {

      ["idEmail"]=>

      int(1)

      ["email"]=>

      string(26) "mailaddres@gmail.com"

      ["ksUtente"]=>

      int(3)

      ["ksAzienda"]=>

      NULL

      ["last_update"]=>

      NULL

    }

    ["_related":"yii\db\BaseActiveRecord":private]=>

    array(0) {

    }

    ["_errors":"yii\base\Model":private]=>

    NULL

    ["_validators":"yii\base\Model":private]=>

    NULL

    ["_scenario":"yii\base\Model":private]=>

    string(7) "default"

    ["_events":"yii\base\Component":private]=>

    array(0) {

    }

    ["_behaviors":"yii\base\Component":private]=>

    array(0) {

    }

  }


  [1]=>

  object(app\models\Email)#87 (<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> {

    ["_attributes":"yii\db\BaseActiveRecord":private]=>

    array(5) {

      ["idEmail"]=>

      int(2)

      ["email"]=>

      string(5) "dasda"

      ["ksUtente"]=>

      int(3)

      ["ksAzienda"]=>

      NULL

      ["last_update"]=>

      NULL

    }

    ["_oldAttributes":"yii\db\BaseActiveRecord":private]=>

    array(5) {

      ["idEmail"]=>

      int(2)

      ["email"]=>

      string(5) "dasda"

      ["ksUtente"]=>

      int(3)

      ["ksAzienda"]=>

      NULL

      ["last_update"]=>

      NULL

    }

    ["_related":"yii\db\BaseActiveRecord":private]=>

    array(0) {

    }

    ["_errors":"yii\base\Model":private]=>

    NULL

    ["_validators":"yii\base\Model":private]=>

    NULL

    ["_scenario":"yii\base\Model":private]=>

    string(7) "default"

    ["_events":"yii\base\Component":private]=>

    array(0) {

    }

    ["_behaviors":"yii\base\Component":private]=>

    array(0) {

    }

  }

}



Among others, I tried to set the following array into the $attributes property of DetailView.




$attributes= [....

[

              'attribute' => 'Email',

               'value'=> function() {

                             return ArrayHelper::htmlEncode (app\models\Utente::getEmails());

                            }

]]



But i get the error:

Any hint? :huh:

Thank you

P.S. Just to say…

an hint can be also “read this wiki”. :)

I red a lot of manuals but still not able to get this work. :wacko:

You can do like this :




use yii\helpers\ArrayHelper;

...

'value' => implode(', ', ArrayHelper::map($model->emails, 'id', 'email')),

...



Or create a magic method __toString() in your Email class :




class Email extends ActiveRecord

{

  public function __toString()

  {

    return $this->email;

  }

}



ohhh. that works great!!! Thank you so much.

I’ll go more deep studying how to properly use the ArrayHelper and the magic functions.

What is strange, now, is that I’d like to have the emails listed one under the other.

I tried with


[

              'attribute' => 'Email',

              'value' => implode("<br> \n", ArrayHelper::map($model->emails, 'idEmail', 'email')),

]

following what is written here but it’s not working.

Just displays the <br> code in the output, either using ‘’ or “”

Thank you for your help.

It’s because by default it’s encoded ("format ‘text’). You have to edit the format like this :




[

  'attribute' => 'Email',

  'value' => implode("<br> \n", ArrayHelper::map($model->emails, 'idEmail', 'email')),

  'format' => 'raw'//or 'html' I think

]



More informations here

Yes!

Thank you so much Timmy ;)

I appreciate :)