Save data in the session

[color="#000080"]

Hello, can anyone help me? - Thanks!

[color="#C0C0C0"]>[/color]I want to save result in session.

[color="#C0C0C0"]>[/color]Putting the following code in UserIdentity.php.[/color]




$Permissoes=Yii::app()->db->createCommand()

   ->select('*')

   ->from('usuario_permissao')

   ->where('IdGrupo=:IdGrupo', array(':IdGrupo'=>1))

   ->query();


Yii::app()->user->setState('Permissao',$Permissoes); 



[color="#C0C0C0"]>[/color][color="#000080"]The result contained in the session I want to introduce entering the following code in the view:[/color]




foreach ( Yii::app()->user->getState('Permissao') as $BuscaPermissao ) { 

      echo $BuscaPermissao['Id']."<br>";

      echo $BuscaPermissao['IdGrupo']."<br>";

      echo $BuscaPermissao['Area']."<br>";

      echo $BuscaPermissao['Acesso']."<br>";

}



[color="#C0C0C0"]>[/color][color="#000080"]when I enter login the system, has an error:[/color]

[color="#FF0000"][size="6"]PDOException[/size][/color]

[color="#696969"]You cannot serialize or unserialize PDOStatement instances[/color]

[color="#000000"]C:\www\martyii\themes\classico\views\layouts\main.php(33)[/color]


[color="#000000"]<div id="mainmenu">

	&lt;?php &#036;this-&gt;widget('zii.widgets.CMenu',array(


		'items'=&gt;array(


			array('label'=&gt;'Home', 'url'=&gt;array('/site/index')),


			array('label'=&gt;'About', 'url'=&gt;array('/site/page', 'view'=&gt;'about')),


			array('label'=&gt;'Contact', 'url'=&gt;array('/site/contact')),

[color="#FF0000"] array(‘label’=>‘Login’, ‘url’=>array(’/site/login’), ‘visible’=>Yii::app()->user->isGuest),[/color]

			array('label'=&gt;'Logout ('.Yii::app()-&gt;user-&gt;name.')', 'url'=&gt;array('/site/logout'), 'visible'=&gt;&#33;Yii::app()-&gt;user-&gt;isGuest)


		),


	)); ?&gt;


&lt;/div&gt;[/color]

Hi,

It’s because the return of Query Builder ->query(), is a kind of resource, resources can’t be serialized and any data in a session is automatically serialized. So how do we fix it? One way is to convert the resource into an array.

Change




Yii::app()->user->setState('Permissao',$Permissoes); 

to:




//add ->readAll() to convert CDbDataReader to an array

Yii::app()->user->setState('Permissao',$Permissoes->readAll()); 

Try it out, hope I’ve helped you.

:)

[color="#0000FF"]Excellent! Thank you.[/color]