Yii Framework Forum: Can Not Refer To Yii::app()->User In Cwebuser (Indirect Calls) - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Can Not Refer To Yii::app()->User In Cwebuser (Indirect Calls) Rate Topic: -----

#1 User is offline   le_top 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 353
  • Joined: 08-June 10
  • Location:France

Posted 28 June 2013 - 09:03 PM

Upon initial login, when the user component is created implicitally through Yii::app()->getComponent('user'), I discovered an error when upgrading a server saying that Yii;;app()->user did not exist.

This is because CModule::getComponent() looks like this:
public function getComponent($id,$createIfNull=true)
{
    if(isset($this->_components[$id]))
        return $this->_components[$id];
    elseif(isset($this->_componentConfig[$id]) && $createIfNull)
    {
        $config=$this->_componentConfig[$id];
        if(!isset($config['enabled']) || $config['enabled'])
        {
            Yii::trace("Loading \"$id\" application component",'system.CModule');
            unset($config['enabled']);
            $component=Yii::createComponent($config);
            $component->init();
            return $this->_components[$id]=$component;
        }
    }
[size=2]}[/size]



By making it look like this, I got it to work:

	public function getComponent($id,$createIfNull=true)
	{
		if(isset($this->_components[$id]))
			return $this->_components[$id];
		else if(isset($this->_componentConfig[$id]) && $createIfNull)
		{
			$config=$this->_componentConfig[$id];
			if(!isset($config['enabled']) || $config['enabled'])
			{
				Yii::trace("Loading \"$id\" application component",'system.CModule');
				unset($config['enabled']);
				$component=Yii::createComponent($config);
				$this->_components[$id]=$component;
				$component->init();
				return $component;
			}
		}
	}


So that assigns the component to its id before starting initialisation.
In fact that is the same order as in 'setComponent':

    {
        $this->_components[$id]=$component;

        if(!$component->getIsInitialized())
            $component->init();

        return;
    }


I haven't tried to preload the 'user' component - that might work too. This issue likely appears for 'user' only in practice.

So if you get a 'CWebApplication::$user' does not exist, you know thtat it may be due to the above.

(Context: From 'afterLogin' in CWebUser, a utility method gets called which determines the current user through Yii::app()->user)
0

#2 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 530
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 18 July 2013 - 11:11 AM

Why should the user component call itself via Yii::app()->user ? You should use $this instead.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users