YII2.0注册表类'用户'不存在或语法错误

时间:2016-02-04 12:24:36

标签: php yii2 gii

我的模型文件夹中有用户模型,我必须通过gii创建一个新的注册表单,以便我在表单创建gii页面中提供我的类名。

但它抛出类名不存在或语法错误。

Users.php代码

namespace app\models;
use Yii;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\helpers\Security;
use yii\web\IdentityInterface;

/**
 * This is the model class for table "users".
 *
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property integer $user_type
 */

class Users extends \yii\db\ActiveRecord implements IdentityInterface
{
/**
 * @inheritdoc
 */
public static function tableName()
{
    return 'users';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['username', 'password', 'user_type'], 'required'],
        [['user_type'], 'integer'],
        [['username', 'password','auth_key'], 'string', 'max' => 255]
    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'id' => 'ID',
        'username' => 'Username',
        'password' => 'Password',
        'user_type' => 'User Type',
    ];
}

/** INCLUDE USER LOGIN VALIDATION FUNCTIONS**/
/**
 * @inheritdoc
 */
public static function findIdentity($id)
{
    return static::findOne($id);
}

/**
 * @inheritdoc
 */
/* modified */
public static function findIdentityByAccessToken($token, $type = null)
{
    return static::findOne(['access_token' => $token]);
}

/* removed
    public static function findIdentityByAccessToken($token)
    {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }
*/
/**
 * Finds user by username
 *
 * @param  string      $username
 * @return static|null
 */
public static function findByUsername($username)
{
    return static::findOne(['username' => $username]);
}

/**
 * Finds user by password reset token
 *
 * @param  string      $token password reset token
 * @return static|null
 */
public static function findByPasswordResetToken($token)
{
    $expire = \Yii::$app->params['user.passwordResetTokenExpire'];
    $parts = explode('_', $token);
    $timestamp = (int) end($parts);
    if ($timestamp + $expire < time()) {
        // token expired
        return null;
    }

    return static::findOne([
        'password_reset_token' => $token
    ]);
}

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

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

/**
 * @inheritdoc
 */
public function validateAuthKey($authKey)
{
    return $this->getAuthKey() === $authKey;
}

/**
 * Validates password
 *
 * @param  string  $password password to validate
 * @return boolean if password provided is valid for current user
 */
public function validatePassword($password)
{
    //return $this->password === sha1($password);
    return $this->password === $password;
}

/**
 * Generates password hash from password and sets it to the model
 *
 * @param string $password
 */
public function setPassword($password)
{
    $this->password_hash = Security::generatePasswordHash($password);
}

/**
 * Generates "remember me" authentication key
 */
public function generateAuthKey()
{
    $this->auth_key = Security::generateRandomKey();
}

/**
 * Generates new password reset token
 */
public function generatePasswordResetToken()
{
    $this->password_reset_token = Security::generateRandomKey() . '_' . time();
}

/**
 * Removes password reset token
 */
public function removePasswordResetToken()
{
    $this->password_reset_token = null;
}
/** EXTENSION MOVIE **/
}

我可以知道这是什么问题,屏幕是为了让您更好地理解。 enter image description here

2 个答案:

答案 0 :(得分:1)

对模型类使用完整的类路径/命名空间,因为在另一个包中可能还有另一个具有相同名称的类,因此您必须非常具体。这完全取决于软件包,{{1}}上的工具提示也说明了(你可以通过悬停在标签Model Class上看到)。请点击此处了解namespaces

答案 1 :(得分:1)

您必须为Model类路径使用正确的语法。你可以通过悬停&#34;模型类&#34;找到正确的路径格式。领域。您必须将模型类路径用作&#34; app \ models \ Users&#34;

尝试一次,如果您仍然面临同样的疑问,请告诉我。