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;
class User extends ActiveRecord implements IdentityInterface
{
public static function tableName()
{
return '{{%user}}';
}
public function behaviors()
{
return [
TimestampBehavior::className(),
];
}
public function rules()
{
return [];
}
public static function findByEmail($username)
{
return static::findOne(['user_email' => $username]);
}
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' => [
'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',
'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?