Creating Reports In Yii

I am attempting to create a simple reports section of an app , i have created a reportscontroller and was wondering if there is any way of importing different models into it to render pdfs based on data from them , is this possible ?

I’ve been dealing with the same need for the past weeks. One possibility is integrating Reportico PHP Report Generator into Yii but that is a project unto itself for which I don’t have the time right now.

What I’ve done is:

  1. Create a ReportsController whose job is only to coordinate report processing using external actions.

  2. Each report is a separate controller action outside of ReportsController - e.g. protected/controllers/actions/reports/Rpt001Action.php, etc. That way one can work on each report individually without messing up any other.

  3. The parameters for each report are similarly collected into its own CForm model - e.g. protected/models/reports/Rpt001ParamsForm.php. Remember to import ‘application.models.reports.*’ in config/main.php.

  4. Same structure for report views: protected/views/reports/…

a) rpt001.php - starting view that renderpartials the report parameters view below

b.) _rpt001params.php - renderpartialed view that collects the report’s parameters in an CActiveForm using model Rpt001ParamsForms.php

c) rpt001results.php - CGridView/CListView/CDetailView/whatever view that displays the report results (which were processed in Rpt001Action)

d) _rpt001criteria.php - nice to have view which is renderpartialed into rpt001results.php so the report can display the criteria it was invoked with.

Going back to Rpt001Action.php it flows as follows:

  1. instance report model & criteria: $model = new Rpt001ParamsForm; $criteria = new CDbCriteria;

  2. if set process parameters (if not jump to step 6): if(isset($_POST[‘Rpt001ParamsForm’])){…} and populate $criteria

  3. instance dataprovider: $dataProvider = new CActiveDataProvider(‘Table’, array(‘criteria’=>$criteria,…

  4. render results: $this->controller->render(‘rpt001results’, ‘dataProvider’=$dataProvider …

  5. Call Yii::app()->end(); to avoid a renderpartial loop

  6. Upon first call (no $_POST content) render the original view rpt001.php.

  7. If you want to have a PDF version of the report add the corresponding controllers/actions/reports/Rpt001pdfAction.php and views/reports/rpt001pdf.php with the logic to output to PDF or whatever. I usually open a new browser window/tab for this upon a button click.

I’m positive there are better ways of doing this, but it works, especially within the timeframe my project requires. Any suggestions are appreciated.

1 Like

Thanks for the informative reply , i’m just trying to get jasper reports working which may work for my situation , i’ll let you know how i get on

No problem. I considered Jasper reports but would prefer a “pure” PHP solution so I don’t overload my affordable virtual server by having to support Java as well. Good luck.

Hi there

There is now a beta test module that integrates Reportico report designer into Yii … its available from the Reportico Web Site

To download or try out the module go to :-

Peter Deed