-2

I have two models(User, ReferralsForm) joined together in my controller. The join works fine but I am having a problem in accessing the attributes in ReferralsForm. Here's the code in my controller:

public function actionReferrals()
{
    $idOfCurrentUser = Yii::$app->user->identity->id;
    $query = User::find()->joinWith('referrals')->where(['userid' => $idOfCurrentUser])->all();

    $model = new User();
    $ref_hash = $model->getHash();

    return $this->render('referrals' ,['query' => $query, 'ref_hash' => $ref_hash]);
}

My view:

        (...)
        <table class="table table-hover">
          <tbody><tr>
            <th>#</th>
            <th>Referred Username</th>
            <th>Status</th>
          </tr>
          <tr>
              <?php foreach ($query as $result){ ?>
            <td><?php echo $result->user_id?></td>
            <td><?php echo $result->display_name?></td>
            <td><?php echo $result->status?></td>
              <?php } ?>
          </tr>
          </tbody>
        </table>
        (...)

Here's the error that I get:

Getting unknown property: app\models\User::status

I wanted to access the status from ReferrasForm however it doesn't include the attributes from ReferralsForm, only the attributes from the Users even if I already joined them and added the all() from similar problems i read.

codex
  • 383
  • 3
  • 14

1 Answers1

2

Use relationName->attributeName

 <td> <?= $result->referrals->status ?> </td>

If you have hasMany() relation then you need to loop through $result->referrals or use $result->referrals[0]->status

Insane Skull
  • 8,810
  • 9
  • 38
  • 59