[size="4"]General[/size]
I have 2 different user tables in my db:
doctor
and
patient
. I created models and CRUD for both tables. Then, I’ve fully completed doctor registration, login. Everything is OK for now. Also registration of patient’s done. But couldn’t login to system as patient.
[size="6"]What steps will reproduce the problem?[/size]
[size="4"]Registration action[/size]
public function actionRegister()
{
// because I was set default identity class to 'frontend\models\Doctor'
// and must be changed to Patient class for registering patients.
Yii::$app->setComponents([
'user' => [
'identityClass' => 'frontend\models\Patient',
'enableAutoLogin' => true,
'class' => 'yii\web\User',
]
]);
$model = new Patient();
if ($model->load(Yii::$app->request->post())) {
// if ($model->validate()) { // creates problem with captcha
if (UploadedFile::getInstance($model, 'file')) {
$model->file = UploadedFile::getInstance($model, 'file');
$model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension);
$model->image = 'uploads/' . $model->file->baseName . '.' . $model->file->extension;
}
if ($user = $model->PatientSignUp(Yii::$app->request->post('blood'), Yii::$app->request->post('sex'), $image = $model->image)) {
if (Yii::$app->getUser()->login($user)) {
Yii::$app->session->setFlash('success', 'Qeydiyyat uğurla başa çatdı.');
//echo Yii::$app->user->getIdentity()->attributes['patient_id']; // this line returns nothing
//exit(); // without this line !!!
return $this->redirect(['index']);
}
} else {
Yii::$app->session->setFlash('error', 'Qeydiyyat zamanı xəta baş verdi.');
return $this->render('create', [
'model' => $model,
]);
}
// }
}
return $this->render('create', [
'model' => $model,
]);
}
[size="5"]PatientSignUp function[/size]
public function PatientSignUp($blood_id, $sex, $image = null)
{
$user = new Patient();
$user->patient_id = $this->generateID();
$user->patient_username = $this->patient_username;
$user->name = $this->name;
$user->surname = $this->surname;
$user->email = $this->email;
$user->address = $this->address;
$user->blood_id = $blood_id;
$user->sex = $sex;
$user->phone = $this->phone;
$user->dob = $this->dob;
$user->image = $image;
$user->setPassword($this->password);
if ($user->save())
return $user;
return null;
}
in my layouts/main.php I set some conditions:
<ul>
........
<li><a href="<?= \yii\helpers\Url::toRoute(['site/contact']) ?>">Contact Us</a></li>
<?php if (!Yii::$app->user->isGuest) {
if (Yii::$app->getComponents()['user']['identityClass'] === 'frontend\models\Doctor') {
echo '<li>' . Html::a(
'Logout (' . Yii::$app->user->identity->doctor_username . ')',
['doctor/logout'],
['data-method' => 'post']) . '</li>';
}
else {
echo '<li>' . Html::a(
'Logout (' . Yii::$app->user->identity->doctor_username/*getIdentity()->attributes['patient_username']*/ . ')',
['patient/logout'],
['data-method' => 'post']) . '</li>';
}
} else {
echo '<li>' . Html::a(
'Login',
['site/login']) . '</li>';
} ?>
.......
</ul>
[size="6"]What is the expected result?[/size]
Shortly: username of patient (string)
The same codes working perfectly: in menu bar I can see
Logout (_doctorusername_)
. But in patient mode it fails (not showing
Logout(_patientusername_)
and Login appears.
[size="6"]What do I get instead?[/size]
nothing (null)
[size="4"]Additional info[/size]
| Yii version | 2.0.8 advanced template |
| PHP version | 7.0.4 |
| DB | MySQL 5.7 |
| Operating system | Windows 10 Home |
| Web server | Apache/2.4.18 |
| Browser Chrome | 50.0.2661.102 m (64-bit) |