سؤال

I am trying to create a PDF from a filtered CGridView. The value will be passed via dropdown in Advanced search but the problem is that i am unable to filter the search by my pdf function.

Controller

public function actionPrint() {
    $mPDF1 = Yii::app()->ePdf->mpdf('ar','A4','14','dejavusanscondensed');
    $model=new Country('search');

    $model->center_id = 1;// This value will be passed from dropdown
                          //and i want the report to be made on this

    $model->unsetAttributes(); 
    if(isset($_GET['Country']))
        $model->attributes=$_GET['Country'];

    $html = '';
    $html .= $this->renderPartial('candidates', array('model'=>$model, 'enablePagination' => false),true);
    $mPDF1->WriteHTML($html, false);
    $mPDF1->Output('list.pdf','D');
}

View

    $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'country-grid',
        'dataProvider'=>$model->search($enablePagination),
        'summaryText' => '',
       // 'enablePagination' => false,
        'filter'=>$model,
        'columns'=>array(
            'name',
            array(
                'header'=>' Total Registered Candidates',
                'value'=>'$data->itemsTotal',
            ),
        ),
    ));
    echo CHtml::link(
        'Save as PDF',
        Yii::app()->createUrl('country/print'),
        array('class'=>'btnPrint btn btn-danger','target'=>'_blank'));

Model

public function search($enablePagination = true)
{
         $criteria->together= true;
        $criteria->with=array('center');
 $criteria->compare('center.name', $this->center_id, true);
..........
        if ($enablePagination)
        {
                $pagination = array(
                        'pageSize' => 30,
                );
        }
        else
        {
                $pagination = false;
        }

        return new CActiveDataProvider($model, array(
                'criteria' => $criteria,
                'pagination' => $pagination,
        ));
}
هل كانت مفيدة؟

المحلول

Since center_id is a foreign key the line

$criteria->compare('center.name', $this->center_id, true);

should read

$criteria->compare('center_id', $this->center_id);

You could also do the following but this adds a condition on the joined table and could lead to slower queries.

$criteria->compare('center.id', $this->center_id);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top