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

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)

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