4

我想扩展我自己的用户模型,使用一个名为 getFirstname 的函数。该函数应返回数据库中的自定义字段。

但是当我扩展用户模型时。它说“调用未知方法:yii\web\User::getFirstname()”

我的用户模型在 app\models\user 中。我已删除文件中与此问题无关的方法。

<?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;
    }  
}

我的配置文件:

$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'),
    ],
];

在我的视图文件中:

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

我在这里做错了什么?

4

2 回答 2

5

尝试这个:

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

<!-- or -->

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

Yii::$app->user您一起获得用户组件User 组件类注释告诉你:

User 是管理用户身份验证状态的“用户”应用程序组件的类。

因此,您获得的不是实际用户Yii::$app->user,而是管理组件。使用identitygetIdentity()使用此类,您将获得实现 IdentityInterface 的用户对象。当您查看时:您的 User 类实现了此接口。在您的配置中,您告诉用户组件它应该完全使用您的用户类。

于 2015-03-10T07:34:51.807 回答
0

User具有该getFirstname方法的类在命名\app\models空间中,但在您的视图中被调用的类在yii\web命名空间中。您应该更改要使用的代码\app\models\User

于 2015-03-10T07:01:31.840 回答