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.
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\n";
return;
Console output:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.