Yii2: calling a user method in the class User from view

I would like to extend my own user model with a function called getFirstname. The function should return a custom field in the database.

But when I expand the user model. It says: "Calling an unknown method: yii \ web \ User :: getFirstname ()"

My user model in the \ models \ user application. I deleted methods in the file that are not related to this problem.

<?php

namespace app\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;

/**
 * User model
 *
 */
class User extends ActiveRecord implements IdentityInterface
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [];
    }

    /**
     * Finds user by email
     *
     * @param string $email
     * @return static|null
     */
    public static function findByEmail($username)
    {
        return static::findOne(['user_email' => $username]);
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->getPrimaryKey();
    }

    public function getFirstname()
    {
        return $this->user_firstname;
    }  
}

My configuration file:

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'thepaXucufrE2pefRethaxetrusacRu3',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'loginUrl' => ['login/sign-in'],
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
    ],
];

And in my file:

<?= Yii::$app->user->getFirstname() ?>

What am I doing wrong here?

+4
source share
2 answers

Try the following:

<?= Yii::$app->user->identity->getFirstname() ?>

<!-- or -->

<?= Yii::$app->user->identity->firstname ?>

Yii::$app->user . :

- "" , .

, , Yii::$app->user, . identity getIdentity() , IdentityInterface. : . , User.

+5

User getFirstname \app\models, , , yii\web. \app\models\User.

0

All Articles