How to check Component Exist in Module?

Hi,

I’m using Yii::app()->hasComponent(‘compName’) to verify Component exists in Module, but it returns FALSE.

The component is not defined in main config file.

Is there any way to solve it?

Thanks!

=======================

Methods tried but fail :-

Method 1




Yii::app()->hasComponent('compName')



Result : return false

Method 2




Yii::app()->getComponents()



Result : Component is not showing in the list, might cause of not declared in main config

Method 3




class MyModule extends CWebModule

{

        public function init()

        {

                $this->setImport(array(

                        'My.models.*',

                        'My.components.*',

                ));

                

                $this->preloadComponents();

        }

}


Yii::app()->getModule('My')->hasComponent('compName');



Result : return false

is this component declared in main config file?

No, it is not defined in Main Config file.

I’m using DB to control which Component to be loaded.

So before "new class", would like to check whether the component is exist to avoid fatal error.

I think u need to import or declare all components in main…or create component something like that…

refer

CModule.hasComponent()

http://www.yiiframework.com/doc/api/1.1/CModule#hasComponent-detail

http://www.yiiframework.com/doc/api/1.1/YiiBase#createComponent-detail

Hi Rajith,

Thanks for your kind reply.

I have tried Yii::app()->hasComponent(), it return nothing.

I tried Yii::app()->getComponents() as well, the expected Component list was not show since they are not declared in main config.

I have done the following setting to MyModule.php file :-




class MyModule extends CWebModule

{

	public function init()

	{

		$this->setImport(array(

			'My.models.*',

			'My.components.*',

		));

		

		$this->preloadComponents();

	}

}



I though it will auto declare all component inside MyModule.

I have tried Yii::app()->getModule(‘My’)->hasComponent(‘compName’), it still return nothing.