【问题标题】:not getting user id from from Auth::instance->get_user()-id in Kohana未从 Kohana 中的 Auth::instance->get_user()-id 获取用户 ID
【发布时间】:2011-07-18 08:55:55
【问题描述】:

我正在使用 kohana 的身份验证模块。我确实注册并登录并且工作正常。但是当我做Auth::instance()->get_user()->id 我得到NULL

登录时,我使用Auth::instance()->login($validator['email'], $validator['password']) 进行操作,然后将用户重定向到主页。

但是当我在其中一个控制器中执行Auth::instance()->get_user()->id 时,我得到NULL

是什么原因。是不是得先设置点什么???

【问题讨论】:

    标签: kohana-3 kohana-orm kohana-auth


    【解决方案1】:

    我的错误

    modules/auth/classes/kohana/auth/orm.php_login函数中

    因为我在做以下事情

    $user = ORM::factory('user');
    $user->where('email', ' = ', $email)
        ->and_where('password', ' = ', $password)
        ->find();
    
    // TODO remember to be done
    if ($user !== null) {
        $this->complete_login($user);
        return true;
    } else {
        return false;
    }
    

    在上面我检查 $user 是否为空,但如果电子邮件和密码不匹配,则将使用所有列的 NULL 值创建用户实例。

    所以现在我正在检查$user->id !== NULL,它工作正常。

    【讨论】:

    【解决方案2】:

    试试Auth::instance()->get_user()->pk()

    pk() 是主键。

    在 KO3 中工作。

    【讨论】:

      【解决方案3】:

      试试这个:

      if ($user->loaded()) {
          $this->complete_login($user);
          return true;
      } else {
          return false;
      }
      

      如果您想知道发生了什么,请参阅ORM::__call()(因为 ORM::loaded() 不存在)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-08-03
        • 2022-01-21
        • 2018-09-24
        • 1970-01-01
        • 2017-12-14
        • 2017-01-18
        • 1970-01-01
        相关资源
        最近更新 更多