Search code examples
phpyiigetstate

Get a list of all state in yii


This is my code:

for ($i=0; $i < count($rights); $i++) {
    $this->setState($rights[$i], true);
}

Here I am setting the setState dynamically, but I want to get all of states in a list. I did not find any references about this. I only found this:

Yii::app()->user->getState('name');

But this is not helping me. How can I get a list of all states in yii? thx


Solution

  • According to the API of CWebUser, there's no function for that purpose.

    According to the code of setState, you can see that it saves the values in a session and that there's no another way of "storing" the used states. (Like an array of all created states)

    public function setState($key,$value,$defaultValue=null)
    {
        $key=$this->getStateKeyPrefix().$key;
        if($value===$defaultValue)
            unset($_SESSION[$key]);
        else
            $_SESSION[$key]=$value;
    }
    

    One solution is manually going through all the existing sessions ($_SESSION) but in my opinion it's not very efficient idea.

    Another solution is to have a property/variable (array) to hold all the states that you've created and than using a foreach loop, use getState.