M
M
Monitorkin2016-02-05 10:11:47
Yii
Monitorkin, 2016-02-05 10:11:47

How to display data from linked table in edit form?

Available:

  • table User with fields id (user id) and org (organization id from table Org) , User model;
  • table Org with fields id (organization id) , name (organization name) , Org model;
  • connection is configured in the User model (done via gii) :
    public function getOrg0()
        {
            return $this->hasOne(Org::className(), ['id' => 'org']);
        }

  • a form for editing a record of the User table: ( app/modules/users/views/user/_form.php) which has a line for editing a field
    /* @var $model app\modules\users\models\User */
    .....
    <?= $form->field($model, 'org0.name')->textInput() ?>

The output is an error:
Getting unknown property: app\modules\users\models\User::org0.name
If we write like this,
<?= $form->field($model, 'org')->textInput() ?>
then the output is the organization id from the User table.
How to write a line correctly so that the output contains the name of the organization from the Org table?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Krivochenko, 2016-02-07
@Monitorkin

<?= $form->field($model->org0, 'name')->textInput() ?>

V
Valery, 2016-02-05
@supervaleha

Can you do it with a getter?
<?= $form->field($model, 'orgName')->textInput() ?>
...................
in the model:
public function getOrgName( ){ return $this->org0->name; }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question