我想让一个登录页面与2个不同的用户级别,医生"家长如何在Yii中实现....
下面列出了两个表....对于两个用户,电子邮件地址都是用户名。
CREATE TABLE Doctor
doctor_email
VARCHAR (50),password
VARCHAR (50),doctor_name
VARCHAR (50),doctor_phone
INT (10),speciality
VARCHAR (200),主键( doctor_email
)
CREATE TABLE Parent
parent_email
VARCHAR (50),parent_name
VARCHAR (50),password
VARCHAR (20),parent_address
VARCHAR (100),parent_phone
INT (10),主键( parent_email
));
这是UserIdentity.php
中的authenticate()函数public function authenticate() {
$record1 = Doctor::model()->findByAttributes(array('doctor_email' => $this->username));
$record2 = Parents::model()->findByAttributes(array('parent_email' => $this->username));
if ($record1 !== NULL) {
if ($record1->password == $this->password) {
$this->errorCode = self::ERROR_NONE;
} else {
$this->errorCode = self::ERROR_PASSWORD_INVALID;
}
} else if ($record2 !== NULL) {
if ($record2->password == $this->password) {
$this->errorCode = self::ERROR_NONE;
} else {
$this->errorCode = self::ERROR_PASSWORD_INVALID;
}
}
return !$this->errorCode;
}
这是我的login()函数在models/loginfo .php
公共函数login() {
if ($this->_identity === null) {
$this->_identity = new UserIdentity($this->username, $this->password);
$this->_identity->authenticate();
if ($this->_identity->record1 !== NULL)
$this->isDoc = TRUE;
else if ($this->_identity->record2 !== NULL)
$this->isDoc = FALSE;
}
if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
$duration = $this->rememberMe ? 3600 * 24 * 30 : 0; // 30 days
Yii::app()->user->login($this->_identity, $duration);
return true;
}
else
return false;
}
这是sitecontoller。php中的actionLogin()
公共功能actionLogin() {$model = new LoginForm;
// if it is ajax validation request
if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if (isset($_POST['LoginForm'])) {
$model->attributes = $_POST['LoginForm'];
if ($model->validate() && $model->login()) {
if ($model->isDoc) {
$this->redirect(array('/doctor/index'));
} elseif ($model->isDoc == FALSE) {
$this->redirect(array('/parents/index'));
}
}
}
// display the login form
$this->render('login', array('model' => $model));
}
当两个用户登录时,重定向到'/parents/index'。谁能告诉我我做错了什么吗?或者有任何想法.....
我可以看到的问题是$model->isDoc
是Null。因为你使用了$model->isDoc == FALSE
我指的是双等号运算符。所以这个条件为真。
Try with $model->isDoc === FALSE
(三重相等运算符)
我很确定它会再次带你到登录表单。
如果我没有错,那么真正的问题仍然存在
if ($this->_identity->record1 !== NULL)
$this->isDoc = TRUE;
else if ($this->_identity->record2 !== NULL)
$this->isDoc = FALSE;
尝试var_dump($this->_identity->record1)
和var_dump($this->_identity->record2)
,并检查它们是否在任何情况下都不是null
或者你可以试试这个
在你的UserIdentity类中你需要声明两个变量
class UserIdentity extends CUserIdentity
{
public $record1;
public $record2;
}
现在在你的authenticate()
change $record1
to $this->record1
and $record2
to $this->record2