I hope you can help me before I'm going crazy. I'm trying to login a user. This is the actionLogin
public function actionLogin() {
$model = new User();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$arrPost = Yii::$app->request->post();
$identity = User::findOne(['email'=>$arrPost['User']['email'],'password'=>$arrPost['User']['password']]);
$identity->id = $identity->getAttribute('id');
$identity->last_name = $identity->getAttribute('last_name');
$identity->first_name = $identity->getAttribute('first_name');
$identity->admin = $identity->getAttribute('admin');
$identity->address = $identity->getAttribute('address');
$identity->ministry = $identity->getAttribute('ministry');
\yii::$app->user->login($identity,86400);
return $this->redirect(Url::to(['app/index']));
} else {
// either the page is initially displayed or there is some validation error
return $this->render("@app/forms/user/login", ['model' => $model]);
}
}
If I'm doing a var_dump after
\yii::$app->user->login
I got the following dump
object(app\models\User)#88 (17) { ["id"]=> int(1) ["first_name"]=> string(12) "Christianfds" ["last_name"]=> string(7) "Köster" ["email"]=> NULL ["admin"]=> bool(true) ["password"]=> NULL ["address"]=> string(16) "Lieber Christian" ["ministry"]=> string(6) "" ["auth_key"]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(8) { ["id"]=> int(1) ["first_name"]=> string(12) "Christianfds" ["last_name"]=> string(7) "Köster" ["email"]=> string(25) "kirche@familie-koester.eu" ["admin"]=> bool(true) ["password"]=> string(5) "chris" ["address"]=> string(16) "Lieber Christian" ["ministry"]=> string(6) "Diakon" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(8) { ["id"]=> int(1) ["first_name"]=> string(12) "Christianfds" ["last_name"]=> string(7) "Köster" ["email"]=> string(25) "kirche@familie-koester.eu" ["admin"]=> bool(true) ["password"]=> string(5) "chris" ["address"]=> string(16) "Lieber Christian" ["ministry"]=> string(6) "Diakon" } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } }
You see, the the fields for id, first_name, last_name etc. are filled. After an redirect to the entry page and doing a var_dump on
\yii::$app->user->identity
I got the following content:
object(app\models\User)#93 (17) { ["id"]=> NULL ["first_name"]=> NULL ["last_name"]=> NULL ["email"]=> NULL ["admin"]=> NULL ["password"]=> NULL ["address"]=> NULL ["ministry"]=> NULL ["auth_key"]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(8) { ["id"]=> int(1) ["first_name"]=> string(12) "Christianfds" ["last_name"]=> string(7) "Koester" ["email"]=> string(25) "kirche@familie-koester.eu" ["admin"]=> bool(true) ["password"]=> string(5) "chris" ["address"]=> string(16) "Dear Christian" ["ministry"]=> string(6) "" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(8) { ["id"]=> int(1) ["first_name"]=> string(12) "Christianfds" ["last_name"]=> string(7) "Koester" ["email"]=> string(25) "kirche@familie-koester.eu" ["admin"]=> bool(true) ["password"]=> string(5) "chris" ["address"]=> string(16) "Dear Christian" ["ministry"]=> string(6) "" } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } }
You see that the contents of id, first_name, last_name etc. are gone. It is possible for to ask if the user is a guest, which is returned as wrong (the user is logged in). But it is not possible for me to get the values from fields like id, first_name, last_name etc, because they are NULL:
Config file for users:
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'enableSession' => true,
],
Can anybody help me before I'm going mad!
Thanks Chris