doutouman6245 2015-06-05 09:49
浏览 37
已采纳

Yii获得当前登录的用户

To get current user I use Yii::app()->user->name it's ok when I'm logged as a user.

But when cron job executin command and try Yii::app()->user->name it's get Property "CConsoleApplication.user" is not defined..

How to get check inside my method does Yii::app() user os defined

I'm try use isset() but still getting same error.

  • 写回答

1条回答 默认 最新

  • dongzaheng4449 2015-06-05 10:09
    关注

    The thing is, you can't use CWebUser in console application.

    If you rely on a component, which needs CWebUser, you'll have to detect this in the component and create some kind of workaround for this case. For example, you could genereate in your scope code an exception part where ensures that Yii::app()->user is not called:

     public function scopes(){
             if (Yii::app() instanceof CConsoleApplication) {
                  $user = array(''); //no condition 
             }else{
                  $user = array(
                            'condition'=>'id='.Yii::app()->user->id, 
                          );
             }
             return array(
                  'user'    => $user,
                  'active'  => array(
                                       'condition'=>'status='.self::STATUS_ACTIVE,
                               ), ...
             );
     }
    

    Another simple isset() check that works for me:

    if (!isset(Yii::app()->user))
            echo "username not set
    ";
    return;
    

    Console output:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理