Repeated dataProvider needs refactoring

I have some duplicated code below, the “$dataProvider=new CActiveDataProvider(‘LocationsImport’” etc. which runs on the index method and is rendered by the index view & on the go method which is executed via ajax.

I want to refactor this so that the code is only in one place.

What is the best way to refactor this?





public function actionGo()

{

  $model=new LocationsImport;

  $model->init();

  $msg = "Failure";

  

  // Peform import

  $model->go();

  

  if($model->completion_state == 1)

  {

      $msg = "Success";

  }

  

  Yii::app()->user->setFlash('locationsImport',"Locations import ".$msg);

  

  $dataProvider=new CActiveDataProvider('LocationsImport',

  array(

      'pagination'=>array('pageSize'=>10,),

      'sort'=>array(

      'defaultOrder' => array('end_time'=>true),

      )

  ));

  

    $this->widget('zii.widgets.grid.CGridView', array(

   'dataProvider'=>$dataProvider,

   'id' => 'locations_import_archive'

  ));

  

	}


	/**

	 * Lists all models.

	 */

   public function actionIndex()

  {

	$dataProvider=new CActiveDataProvider('LocationsImport',array(

      'pagination'=>array(

        'pageSize'=>10,

      ),

      'sort'=>array(

      'defaultOrder' => array('end_time'=>true),

      )

  ));

		$this->render('index',array(

			'dataProvider'=>$dataProvider,

		));

	}



Create a method in the controller or in the LocationsImport model. It will return the data provider.




public function actionIndex()

{

  $this->render(

    'index',

    array('dataProvider'=>$this->getMyDataProvider()

  ));

}


or


public function actionIndex()

{

  $this->render(

    'index',

    array('dataProvider'=>LocationsImport::model()->getMyDataProvider()

  ));

}



In the actionGo you can call the method on the existing $model.

/Tommy

Hi Tommy,

Thanks for the quick reply. Your post solves the repetition of the data controller but I am still left with


$this->widget('zii.widgets.grid.CGridView'

in the index view and in the actionGo.

How can this also be incorporated?

You can try to put the CGridView in a partial view and call it from actionGo and from the ‘index’ view. I can’t tell for sure if it will work flawlessly (have to try the “load grid view via ajax” part myself, some time).

index.php




$this->renderPartial('_grid', array('dataprovider'=>$dataprovider), false, true);



actionGo




$this->renderPartial('_grid', array('dataprovider'=>$dataprovider));


or perhaps


echo $this->renderPartial('_grid', array('dataprovider'=>$dataprovider), true);



If the grid doesn’t work after being reloaded, search the forum for ‘ajax’, ‘renderPartial’, ‘processOutput’.

Edit:

If the purpose of actionGo is to just update the grid there’s javascipt support for this loaded with CGridView (IIRC fn.yiiGridView.update). Again, search the forum and/or consult the API reference.

/Tommy

Hi,

Thanks, this works - to clarify I used:

index.php (view):


$this->renderPartial('_grid', array('dataprovider'=>$dataprovider), false, true);

actionGo():


$this->renderPartial('_grid', array('dataprovider'=>$dataprovider));