Can I save arrays via the saveState method?

Sorry I meant setState. I can’t seem to edit the title to fix.

In UserIdentity.php I have the following:


     public function authenticate()

    {

...

        $adminUsers=array();

        foreach ($admins as $u) {

            array_push($adminUsers,$u['username']);

        } 

        $this->setState('memberId', $user->memberId);

        $this->setState('adminusers', &$adminUsers);

        $y=$this->getState('adminusers');

        $x=print_r($y,true);

        fwrite($fh,"Data is ".$x."\n");



The output looks fine. The array contains the correct 3 entries which are

strings.

The controller class looks like:




 class FcarcMembersController extends Controller

{

...

	public function accessRules()

	{

            $user=Yii::app()->user;

            $adminusers = $user->getState('adminusers');

            $m = $user->getState('memberId');

            $myFile = "/tmp/artestFile.txt";

            $fh = fopen($myFile, 'w');

            $x=print_r($adminusers);

            fwrite($fh," A Data is ".$x."\n");

            fwrite($fh," R Data is ".$m."\n");



However the output from these write statements show the memberId is OK but the

output from the print_r is "1". It seems that the array was only temporarily

stored in the setState while a string is permanent.

I’m guessing I’m doing something foolish, but I can’t figure out what. Suggestions? The api documentation for setState is rather sparse. I didn’t know if I needed to dereference the $adminUsers array or not, but I tried it both ways with identical results.

Thanks,

Jim.

It’s because you are passing the array by reference &$adminUsers… so this works as long as $adminUsers has values…

try with


$this->setState('adminusers', $adminUsers);

NOTE: without the &

I had tried it without the & originally and it didn’t work. However it must have been something else I corrected in the mean time, 'cause it works now.

To expand on your comment that it works as long as the array has values, if it is empty, anonymous users can access anything. Don’t ask how I found that out. ;)

Thanks,

Jim.