Mengelola User dengan 2 Tabel

Selamat siang… salam kenal semuanya buat seluruh anggota di forum ini…

saya anggota baru di forum ini, dan saya sangat tertarik untuk belajar yii, mau tanya nih… buat para2 suheng dan master2 yii, pertanyaaanya… bagimana menggabungkan 2 tabel untuk untuk dipakai login… begini nih:

ada 2 tabel :

  • tabel user (ID, Username, Password, Email)

  • tabel mahasiswa (NPM, Nama, Telp, Alamt, dll, ID, Username, Password)

saat ini proses login saya menggunakan data user yang ada di tabel user, bagaimana supaya Username yang ada di tabel mahasiswa bisa digunakan untuk juga login. trims… mohon pencerahaannya…?

tinggal di sqlnya aja gan dipakein join…

buat validasinya…

selesai deh semua.

mungkin pake if gan…

jadi dicocokin dulu ke tabel user kalo ga ada baru dicocokin ke tabel mahasiswa…


$record=User::model()->findByAttributes(array('username'=>$this->username,'password'=>$password));

      	if($record===null){

$record=Mahasiswa::model()->findByAttributes(array('username'=>$this->username,'password'=>$password));

if($record===null){

 $this->errorCode=self::ERROR_USERNAME_INVALID;

} else  ........

not testing…

cuma pake logika c…

ok thanks… mau coba dulu nih…

di site controllernya gan…

mau tanya ga… buat kondisinya dimana ya… di sitecontrollernya atau di loginnya

ini isi site controlernya gan :





<?php


/**

 * LoginForm class.

 * LoginForm is the data structure for keeping

 * user login form data. It is used by the 'login' action of 'SiteController'.

 */

class LoginForm extends CFormModel

{

	public $username;

	public $password;

	public $rememberMe;


	private $_identity;

	




	/**

	 * Declares the validation rules.

	 * The rules state that username and password are required,

	 * and password needs to be authenticated.

	 */

	public function rules()

	{

		return array(

			// username and password are required

			array('username, password', 'required'),

			// rememberMe needs to be a boolean

			array('rememberMe', 'boolean'),

			// password needs to be authenticated

			array('password', 'authenticate'),

		);

	}


	/**

	 * Declares attribute labels.

	 */

	public function attributeLabels()

	{

		return array(

			'rememberMe'=>'Remember me next time',

		);

	}


	/**

	 * Authenticates the password.

	 * This is the 'authenticate' validator as declared in rules().

	 */

	public function authenticate($attribute,$params)

	{

		if(!$this->hasErrors())

		{

			$this->_identity=new UserIdentity($this->username,$this->password);

			if(!$this->_identity->authenticate())

				$this->addError('password','Incorrect username or password.');

		}

	}


	/**

	 * Logs in the user using the given username and password in the model.

	 * @return boolean whether login is successful

	 */

	public function login()

	{

		if($this->_identity===null)

		{

			$this->_identity=new UserIdentity($this->username,$this->password);

			$this->_identity->authenticate();

		}

		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;

	}

}






ini loginnya :




<?php


/**

 * LoginForm class.

 * LoginForm is the data structure for keeping

 * user login form data. It is used by the 'login' action of 'SiteController'.

 */

class LoginForm extends CFormModel

{

	public $username;

	public $password;

	public $rememberMe;


	private $_identity;

	




	/**

	 * Declares the validation rules.

	 * The rules state that username and password are required,

	 * and password needs to be authenticated.

	 */

	public function rules()

	{

		return array(

			// username and password are required

			array('username, password', 'required'),

			// rememberMe needs to be a boolean

			array('rememberMe', 'boolean'),

			// password needs to be authenticated

			array('password', 'authenticate'),

		);

	}


	/**

	 * Declares attribute labels.

	 */

	public function attributeLabels()

	{

		return array(

			'rememberMe'=>'Remember me next time',

		);

	}


	/**

	 * Authenticates the password.

	 * This is the 'authenticate' validator as declared in rules().

	 */

	public function authenticate($attribute,$params)

	{

		if(!$this->hasErrors())

		{

			$this->_identity=new UserIdentity($this->username,$this->password);

			if(!$this->_identity->authenticate())

				$this->addError('password','Incorrect username or password.');

		}

	}


	/**

	 * Logs in the user using the given username and password in the model.

	 * @return boolean whether login is successful

	 */

	public function login()

	{

		if($this->_identity===null)

		{

			$this->_identity=new UserIdentity($this->username,$this->password);

			$this->_identity->authenticate();

		}

		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;

	}

}



di UserIdentity.php yg ada folder protected/component

di function authenticate()

lo waktu buat pake tabel user emang yg di edit mana???

useridentity kan???

sori gan… baru inget…iya dah dicek di useridentity, cuma tinggal ngatur kondisinya…nih.,…