T
T
TeslaFeo2019-12-14 17:40:48
Yii
TeslaFeo, 2019-12-14 17:40:48

How can I correctly form a request to display data through fields?

$user = User::find()->where(['username' => $login])->with(['user', 'person'])->one();
how do I correctly form a request to display data through
fields
UserController.php

<?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.'));

    }





}

person.php
<?php

namespace common\models\user;

use Yii;

/**
 * This is the model class for table "persone".
 *
 * @property int $id
 * @property int $user_id
 * @property double $balance
 * @property double $balance_in
 * @property double $balance_out
 * @property double $credit
 * @property int $refovod
 * @property double $rating
 * @property string $referrer
 * @property int $bonus_count
 * @property int $autoriz
 *
 * @property User $user
 */
class Person extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'persone';
    }

    public function fields()
    {
        return [
            'balance',
            'credit',
            'balance',
            'refovod',
            'rating',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_id', 'refovod'], 'integer'],
            [['balance', 'credit', 'rating'], 'number'],
            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']],
        ];
    }
    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'user_id' => Yii::t('app', 'User ID'),
            'balance' => Yii::t('app', 'Balance'),
            'credit' => Yii::t('app', 'Credit'),
            'refovod' => Yii::t('app', 'Refovod'),
            'rating' => Yii::t('app', 'Rating'),
        ];
    }
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::class, ['id' => 'user_id'])->inverseOf('person');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getRefovodUser()
    {
        return $this->hasOne(User::class, ['id' => 'refovod']);
    }



}

user.php
<?php
/**
 * Created by PhpStorm.
 * User: TOSHIBA-PC
 * Date: 13.05.2018
 * Time: 19:31
 */

namespace common\models\user;

use common\models\Todo;
use common\services\cookies\CookieService;
use Yii;

class User extends \dektrium\user\models\User
{
    private $person;

    public $cookieParams;

    public static function tableName()
    {
        return 'user';
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPerson()
    {
        return $this->hasOne(Person::class, ['user_id' => 'id'])->inverseOf('user');
    }

    /** @inheritdoc */
    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        if ($insert) {
            if (!$this->person) {
                $this->person = Yii::createObject(Person::class);
            }

            if (\Yii::$app->id == 'app-frontend') {
                $this->cookieParams = \Yii::$app->params['cookies'];
                $this->setRefovod();
                $this->setReferrer();
            }

            $this->person->link('user', $this);
        }
    }

    /**
     * Устанавливает рефовода
     * @inheritdoc
     */
    public function setRefovod()
    {
        $service = new CookieService([
            'name' => $this->cookieParams['refovod']['name'],
        ]);

        $this->person->refovod = $service->getValue();
    }

    /**
     * Устанавливает реферрер
     * @inheritdoc
     */
    public function setReferrer()
    {
        $service = new CookieService([
            'name' => $this->cookieParams['referrer']['name'],
        ]);
        $this->person->referrer = $service->getValue();
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTodo()
    {
        return $this->hasMany(Todo::class, ['user_id' => 'id']);
    }

    public function fields()
    {
        return ['id', 'email', 'username', 'rating'];
    }

    public function extraFields()
    {
        return ['profile'];
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question