T
T
TeslaFeo2019-12-13 22:48:09
Yii
TeslaFeo, 2019-12-13 22:48:09

How to display data from a linked table?

<?php


namespace api\modules\v1\controllers;

use api\filters\Auth;
use common\models\HistoryRating;
use common\models\user\Person;
use common\models\user\User;
use Yii;
use yii\data\ActiveDataProvider;
use yii\rest\Controller;
use yii\web\NotFoundHttpException;
use yii\db\ActiveQuery;

class UserController extends Controller
{


    public function actionWall($login){
        $user = Yii::$app->user;
        $user = User::find()->where(['username' => $login])->with(['user', 'person'])->one();
        if ($user) {
            return $user;
        }
        throw new NotFoundHttpException(Yii::t('app', 'The requested user does not exist.'));

    }





}

with(['user', 'person']) works fine without this, but I can't retrieve the data from the associated table
public function fields()
    {
        return ['id', 'email', 'username', 'rating'];
    }

rating is a field from the relational table persone

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@coderiter, 2019-12-14
_

Try to get the necessary models with separate requests, and then mold them into a common array and return them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question