Extending CListView and force it use view file from same folder

Hi to All,

I am extending CListView to as MyProductListView as follows




Yii::import('zii.widgets.CListView');


class PDProductListView extends CListView{

    

    /**

     * Product Category

     */

    public $id_category;

    

    public function init(){

        $dataProvider=new CActiveDataProvider('Product');

        $criteria = new CDbCriteria();

        $criteria->condition = 'id_category=:id_category';

        $criteria->params = array(':id_category'=>$this->id_category);

        

        $dataProvider->setCriteria($criteria);

        

        $this->dataProvider = $dataProvider;

        

        $this->itemView = '_view';

        parent::init();

    }

    

}



It’s working fine. Now I want that this widget pick view file from same folder where this file is. How to do this? Right now it required me to put view file in controller’s view folder.

Thanks

As you can see in the CListView source code the <code>$itemView</code> is used in the CWidget::render() function.

Check and you will figure it out.

Yes I already looked into to the CWidget class and it says about getViewPath() method -




The default implementation returns the 'views' subdirectory of the directory containing the widget class file.



so I created a directory "views" near widget class and put my view file there. But still that not working.

Below is the exception that I am getting




SiteController cannot find the requested view "_view".


E:\htdocs\framework\web\CController.php(875)


863     {

864         if(($viewFile=$this->getViewFile($view))!==false)

865         {

866             $output=$this->renderFile($viewFile,$data,true);

867             if($processOutput)

868                 $output=$this->processOutput($output);

869             if($return)

870                 return $output;

871             else

872                 echo $output;

873         }

874         else

875             throw new CException(Yii::t('yii','{controller} cannot find the requested view "{view}".',

876                 array('{controller}'=>get_class($this), '{view}'=>$view)));

877     }

878 

879     /**

880      * Renders a named clip with the supplied parameters.

881      * This is similar to directly accessing the {@link clips} property.

882      * The main difference is that it can take an array of named parameters

883      * which will replace the corresponding placeholders in the clip.

884      * @param string $name the name of the clip

885      * @param array $params an array of named parameters (name=>value) that should replace

886      * their corresponding placeholders in the clip

887      * @param boolean $return whether to return the clip content or echo it.




From above exception it looks like, it is ignoring widget getViewPath() method and trying to find view file in controller’s view folder.

Ok,

I figure it out myself but not sure if it is correct way.

In CListView, method renderItems(), it is rending view in the context of owner (Calling Controller)




$owner->$render($this->itemView,$data);



So I just override the method getOwner in my widget class as follows -




function getOwner(){

    return $this;

}



Now everything working fine.

You see, it was so simple. :)