Liangjh  
          
              
                August 29, 2013,  9:40am
               
              1 
           
         
        
          Hi everyone. I have a model called "Notification". It contains a "data" field and a "template" field. "data" stores serialized variables.  and  "template"  can locate a view file. So I call renderPartial($template,array(data=>$data),true). and get the whole notification.
My question is: where should I place this code? In model Notification? or a component? or extension?
Thanks.
         
        
           
         
            
       
      
        
          
          
            konapaz  
          
              
                August 29, 2013,  9:54am
               
              2 
           
         
        
          Hi
render method called in controller/action
renderPartial method called both controller/action and in views file
if you create an extension like a CWidget then it could be inside of the run method
         
        
           
         
            
       
      
        
          
          
            paalgg  
          
              
                August 29, 2013, 11:22am
               
              3 
           
         
        
          
According to the idea behind the MVC architecture, the controller should get the data needed from the model and the controller should give the data to a view.
I would solve it something like this (in a controller):
public function actionNotice($id) {
  $model = $this->loadModel($id); /* @var $model Notice */
  if (!is_null($model) { // if the model exists
    // Render the template from the model/Notice, give the whole model/Notice to the template as argument
    $this->render($model->template, array('model'=>$model);
  }
}
In the view/template you output what should be outputted and use $model to get your data.
         
        
           
         
            
       
      
        
          
          
            Liangjh  
          
              
                August 29, 2013, 12:30pm
               
              4 
           
         
        
          replies come fast. thanks   KonApaz and  Paul G.
That is how I deal with it.
since my notifications appear only in one page.I do it in the view file;
views/notifications/index.php
$this->widget('zii.widgets.CListView', array(
	'dataProvider'=>$dataProvider,
	'itemView'=>'_view',
	'separator'=>'<hr/>'
));
the _view.php
$this->renderPartial($data->template,array("data"=>$data->getData()));
the getData() return the unserialized data field.
and in $data->template, for example post_comment.php, I do it
<?php $comment = PostComment::model()->findByPk($data['commentid']);?>
<?php echo CHtml::link($comment->user->username,array("/u/index",'id'=>$comment->userid));?>commented your post
<?php echo CHtml::link($comment->post->title,array('post/view','id'=>$comment->post->postid));?>
So I call renderPartial in _view.php. Untill now, It works.
But still there are 1 problem (or maybe it’s not a problem):I retrive the database in view file. Do you do this sometimes?
         
        
           
         
            
       
      
        
          
          
            konapaz  
          
              
                August 30, 2013, 10:19am
               
              5 
           
         
        
          
 Liangjh:
 
replies come fast. thanks   KonApaz and  Paul G.
That is how I deal with it.
since my notifications appear only in one page.I do it in the view file;
views/notifications/index.php
$this->widget('zii.widgets.CListView', array(
	'dataProvider'=>$dataProvider,
	'itemView'=>'_view',
	'separator'=>'<hr/>'
));
the _view.php
$this->renderPartial($data->template,array("data"=>$data->getData()));
the getData() return the unserialized data field.
and in $data->template, for example post_comment.php, I do it
<?php $comment = PostComment::model()->findByPk($data['commentid']);?>
<?php echo CHtml::link($comment->user->username,array("/u/index",'id'=>$comment->userid));?>commented your post
<?php echo CHtml::link($comment->post->title,array('post/view','id'=>$comment->post->postid));?>
So I call renderPartial in _view.php. Untill now, It works.
But still there are 1 problem (or maybe it’s not a problem):I retrive the database in view file. Do you do this sometimes?
 
 
According to MVC you should get the data in controller and pass it in the view file.
In which place you retrieve the data from database in the view file? give us the code please
         
        
           
         
            
       
      
        
          
          
            Liangjh  
          
              
                August 30, 2013, 11:35am
               
              6 
           
         
        
          
My "data" field look something  like this:  a:1:{s:9:\"commentid\";s:2:\"26\";} ( a serializationg of array(commentid=>9))…
I retrieve a dataProvider in controller. and then .
I get the user model in the template file (a view file). i.e. $data->template, for example post_comment.php, I do this:
<?php $commentid = unserialize(stripslashes($data->data));
$comment = Comment::model()->findByPk(commentid) ?>