[ASK]How to returning the IDROLE from the database?

I create the yii application for my final project. But i found the error result in the returning the IDROLE from my database. In the user identity page, i returning this into the $_idrole variable.

Quote

class UserIdentity extends CUserIdentity

{

        …

      public $_idrole;

public function authenticate()





{





.....





$this->_id=$user->IDUSER;

  $this->_idrole=$user->IDROLE;

        $this->username=$user->USERNAME;

	.....</div></div>

Then in the Login form i returning again like this:

Quote

class LoginForm extends CFormModel

{

        public $idrole;

        public function authenticate($attribute,$params)

{

              …

              case UserIdentity::ERROR_NONE:

&nbsp; &nbsp; &nbsp; $this-&gt;idrole=$identity-&gt;_idrole;

              …

Then in the site controller:

Quote

class SiteController extends CController

{

        private $id_role;

        …

        public function actionLogin()

{





	$form=new LoginForm;

              if(isset($_POST['LoginForm']))

	{

                $this->id_role=$form->idrole;

                    $form->attributes=$_POST['LoginForm'];

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  ......</div></div>

And when i returning in the layouts/main page

Quote

.....

<div id="sidebar1">

<?php $this->widget(‘MemberMenu’,array(‘visible’=>(!Yii::app()->user->isGuest && $this->id_role==3))); ?>

the value always returning NULL. How can i returning this value in the layouts/main? ??? Please help me. thank’s.

The assignment  $this->id_role=$form->idrole; should occur AFTER you call validate() method of the form because $form->idrole is populated in authenticate method which is called by validate().

Pardon… so how is the correct form? can you fix my attachemnt form? thx…

Modify your SiteController.php file.

Move the following line after the validate() call.

$this->_idrole=$user->IDROLE;

Thank’s…  :D

I modified like this:

Quote

if($form->validate())

                        {

			$this-&gt;redirect(Yii::app()-&gt;user-&gt;returnUrl);

                               

                        }

                        $this->id_role=$form->idrole;

or like this:

Quote

if($form->validate())

                        {

			$this-&gt;redirect(Yii::app()-&gt;user-&gt;returnUrl);

                                $this->id_role=$form->idrole;

                        }

but it still doesn't  work… the $id_role still returning NULL in the main site and make my portlets does't appears…

redirect() will terminate current request and redirect to another page. Code after this line will not be executed. Even if it is executed, $this->id_role won't be available in the next request. You need to store it in session.

ok, i’ll try… thank’s  :)

Mr. Qiang or somebody, please help me.

I'm being frustated with this project. I'm purely newbie in the php programmming. So i didn't know how to fix this. please help me to fix this project. i don'd know what should i do… thanks…

You may try the following code. After redirection, if you want to access the role ID, you can use $this->roleID (because we have getter 'getRoleID' defined):



<?php


public function actionXyz()


{


	......


	$form->attributes=$_POST['FormClass'];


	if($form->validate())


	{


		$_SESSION['RoleID']=$form->idrole;


		$this->redirect(...);


	}


}





public function getRoleID()


{


	return isset($_SESSION['RoleID']) ? $_SESSION['RoleID'] : null;


}


Is this correct?

Quote

public function actionLogin()
{





	$form=new LoginForm;

                // collect user input data

	if(isset($_POST&#91;&#039;LoginForm&#039;]))





	{	

                        $form->attributes=$_POST['LoginForm'];

		// validate user input and redirect to previous page if valid





		if($form-&gt;validate())

                        {

                                $_SESSION['RoleID']=$form->idrole;

                                $this->redirect(Yii::app()->user->returnUrl);

                               

                        }

                }

	// display the login form





	$this-&gt;render(&#039;login&#039;,array(&#039;form&#039;=&gt;$form));

               

}

        public function getRoleID()

        {

                return isset($_SESSION['RoleID']) ? $_SESSION['RoleID'] : null;

        }

and in layout/main:

Quote

<?php $this->widget('MemberMenu',array('visible'=>(!Yii::app()->user->isGuest && $this->RoleID=='3'))); ?>

                        <?php $this->widget('AdminMenu',array('visible'=>(!Yii::app()->user->isGuest && $this->RoleID=='1' ))); ?>

                        <?php $this->widget('ProdusenMenu',array('visible'=>(!Yii::app()->user->isGuest && $this->RoleID=='2' ))); ?>

But this still doesn’t work… The RoleID returning Null again…  :-\

Your code seems to be fine.

Could you please check if $form->idrole is null or not? You may insert a die($form->idrole) statement before storing it in $_SESSION.

i already checked it before. it's fine. that's returning a correct value.

I don't know what's wrong with my code. half of this day i try to fix it, hopeless.

But this morning, i don't know why, it's success.

Thanks Mr. Qiang.  ;D