mootensai / yii2-enhanced-gii

Enhanced Yii2 Gii (generator) that generates related Models & CRUD
MIT License
203 stars 98 forks source link

Error when the instance of a model is created - Softdelete and Blameable Behavior #148

Open MarcoPro opened 6 years ago

MarcoPro commented 6 years ago

When I use a model in a console method with softdelete or Blameable Behavior (\Yii::$app->user->id), the following error appears:

'Getting unknown property: yii \ console \ Application :: user'

I have solved it by adding the user component in the console and manually adding the identity in the console command, but I do not know if it is the most appropriate solution.

console/config/main.php

 'components' => [
 .........
 ......
        'user' => [
            'class' => 'yii\web\User',
            'identityClass' => 'app\models\User',
            //'enableAutoLogin' => true,
        ],
 .......
] 

console method

$identity = \app\models\User::findIdentity('<service_user_id>');
if ($identity === null) {
   throw new ForbiddenHttpException;
}
\Yii::$app->user->setIdentity($identity);

Any suggestions?

MarcoPro commented 5 years ago

Has anyone else had this problem?