Problems With Nested Modules

I have admin modules and I need to add comments module [comments-module] as sub modules of admin.

Here I show you the layout of my Project …




application 

   ...

   modules

      ...

      admin

         ...

         modules

            comments



In the main config I’ve add configuration to add admin modules and it worked fine. And then I add comments module as the child modules of admin …




'modules'=>array(

    'admin'=>array(

            'modules'=>array(

                'comments'=>array(

                    //COMMENTS module

                    'defaultModelConfig' => array(

                        //only registered users can post comments

                        'registeredOnly' => false,

                        'useCaptcha' => false,

                        //allow comment tree

                        'allowSubcommenting'=> true,

                        //display comments after moderation

                        'premoderate' => false,

                        //action for postig comment

                        'postCommentAction' => 'admin/comments/comment/postComment',

                        //super user condition(display comment list in admin view and automoderate comments)

                        'isSuperuser'=>'Yii::app()->user->isAdmin===true',

                        //order direction for comments

                        'orderComments'=>'DESC',

                    ),

                    //config for user models, which is used in application

                    'userConfig'=>array(

                        'class'=>'User',

                        'nameProperty'=>'username',

                        'emailProperty'=>'email',

                    ),

                ),

            ),

),



And I’ve add comments as sub modules too in the init function of AdminModules.php




$this->setModules(array('comments'));



But, all of the steps above doesn’t work when I need to accessing the comments modules with this url:


http://myproject.local/admin/comments

I got error message from comment models:


Trying to get property of non-object


/home/tonier/web-prj/tario/protected/modules/admin/modules/comments/models/Comment.php(110)


098 

099         return $rules;

100     }

101 

102     /**

103      * @return array relational rules.

104      */

105     public function relations() {

106         $relations = array(

107             'parent' => array(self::BELONGS_TO, 'Comment', 'parent_comment_id'),

108             'childs' => array(self::HAS_MANY, 'Comment', 'parent_comment_id'),

109         );

110         $userConfig = Yii::app()->getModule('comments')->userConfig;

111         //if defined in config class exists

112         if (isset($userConfig['class']) && class_exists($userConfig['class'])) {

113             $relations = array_merge($relations, array(

114                 'user' => array(self::BELONGS_TO, $userConfig['class'], 'creator_id'),

115                     ));

116         }

117         return $relations;

118     }

119 

120     public function behaviors() {

121         return array(

122             'CTimestampBehavior' => array(

Stack Trace

#0	

+  /home/tonier/web-prj/yii/framework/db/ar/CActiveRecord.php(2288): Comment->relations()

#1	

+  /home/tonier/web-prj/yii/framework/db/ar/CActiveRecord.php(383): CActiveRecordMetaData->__construct(Comment)

#2	

+  /home/tonier/web-prj/yii/framework/db/ar/CActiveRecord.php(398): CActiveRecord::model("Comment")

#3	

+  /home/tonier/web-prj/yii/framework/db/ar/CActiveRecord.php(78): CActiveRecord->getMetaData()

#4	

–  /home/tonier/web-prj/tario/protected/modules/admin/modules/comments/controllers/CommentController.php(102): CActiveRecord->__construct("search")

097     /**

098      * Manages all models.

099      */

100     public function actionAdmin()

101     {

102         $model=new Comment('search');

103         $model->unsetAttributes();  // clear any default values

104         if(isset($_GET['Comment']))

105             $model->attributes=$_GET['Comment'];

106 

107         $this->render('admin',array(

#5	

+  /home/tonier/web-prj/yii/framework/web/actions/CInlineAction.php(50): CommentController->actionAdmin()

#6	

+  /home/tonier/web-prj/yii/framework/web/CController.php(309): CInlineAction->runWithParams(array("language" => "id"))

#7	

+  /home/tonier/web-prj/yii/framework/web/filters/CFilterChain.php(134): CController->runAction(CInlineAction)

#8	

+  /home/tonier/web-prj/yii/framework/web/filters/CFilter.php(41): CFilterChain->run()

#9	

+  /home/tonier/web-prj/yii/framework/web/CController.php(1146): CFilter->filter(CFilterChain)

#10	

+  /home/tonier/web-prj/yii/framework/web/filters/CInlineFilter.php(59): CController->filterAccessControl(CFilterChain)

#11	

+  /home/tonier/web-prj/yii/framework/web/filters/CFilterChain.php(131): CInlineFilter->filter(CFilterChain)

#12	

+  /home/tonier/web-prj/yii/framework/web/CController.php(292): CFilterChain->run()

#13	

+  /home/tonier/web-prj/yii/framework/web/CController.php(266): CController->runActionWithFilters(CInlineAction, array("accessControl", "ajaxOnly + PostComment, Delete, Approve"))

#14	

+  /home/tonier/web-prj/yii/framework/web/CWebApplication.php(283): CController->run("")

#15	

+  /home/tonier/web-prj/yii/framework/web/CWebApplication.php(142): CWebApplication->runController("admin/comments")

#16	

+  /home/tonier/web-prj/yii/framework/base/CApplication.php(162): CWebApplication->processRequest()

#17	

–  /home/tonier/web-prj/tario/index.php(33): CApplication->run()

28     defined('YII_DEBUG') or define('YII_DEBUG',true);

29     // specify how many levels of call stack should be shown in each log message

30     defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

31 }

32 require_once($yii);

33 Yii::createWebApplication($config)->run();



As the highlight error of above error messages were in the line of 110, this code …


110         $userConfig = Yii::app()->getModule('comments')->userConfig;

When I see the trace messages, I found if comment model in comments module, try to get module instance with commend Yii::app()->getModule(‘comments’), but YII doesn’t create instance of the comments module.

Please somebody, What is wrong with my code or setting? Because when I try to use comments module separated from admin, it works well. And I think I’ve following direction from the YII manual books about how to use sub modules correctly.

I found the solution, use




$commentsModule = Yii::app()->getModule('comments');

if($commentsModule === null)

     $commentsModule = Yii::app()->controller->module;



But it just weird, because when I try to trace the process, I found if comments module loaded by YII. The questions is, why Yii::app()->getModule(‘comments’) command failed when comments module became the child module.