Search code examples
phpyiifiltercgridview

Yii search filter is not Working


this is my view file

.........

'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
    array(
            'header' => 'Order ID',
            'name' => 'order_id',
            'type' => 'raw',
            'value' => 'Order::getorderid($data->order_id)',
        ),
    ),
));

this is my file controller.php file for search function

public function actionSearch()
{
    $model = new Order('search');
    $model->unsetAttributes();  // clear any default values 
    if(isset($_GET['Order'])) {
        $model->attributes = $_GET['Order'];
        //echo "pre"; print_r($_GET); exit;
    }
    $this->render('search',array(
        'model'=>$model,
    ));     
}

this is the model file.php

public function search()
{
    $criteria=new CDbCriteria;      
    $criteria->select='t.*';
    $criteria->compare('t.order_id',$this->order_id,true);
    $criteria->compare('t.payment_firstname',$this->payment_firstname,true);
    $criteria->compare('t.telephone',$this->telephone,true);
    $criteria->compare('t.email',$this->email,true);
    $criteria->compare('t.payment_address_1',$this->payment_address_1,true);
    $criteria->compare('t.tracking_id',$this->tracking_id,true);
    $criteria->join = 'left join order_product op on op.order_id =     t.order_id where  t.order_type IN (3) group by t.order_id';
    $criteria->together = true;
    return new CActiveDataProvider($this, array(
    'criteria'=>$criteria,
    'sort'=>array(
        'defaultOrder'=>'order_id DESC',
    ),
    'pagination'=>array(
            'pageSize' => 40,
        ),
    ));
}

I don't see any errors in console.. $_GET is returning the exact the field, but after searching for order_id it is displaying all the orders..

where have this gone wrong ?


Solution

  • Check these code this is what i am using (DB:Postgresql)

    Controller

    public function actionEstimate()
        {
            $model=new SalesEstimate();
                    $model->unsetAttributes();
                    if(isset($_GET['SalesEstimate']))
                    {
                       $model->setAttributes($_GET['SalesEstimate']);
    
                    }
                    $this->render('estimate', array('model'=>$model));
        }
    

    Model

    public function search()
        {
            $command =Yii::app()->db->createCommand("SELECT COUNT(*) FROM sp_sales_estimate_search(:enquiry_id,:ref_no,:estimate_date,:property_id,:amount,:status)");
            $command->bindParam(":enquiry_id",$this->enquiry_id,PDO::PARAM_STR);
            $command->bindParam(":ref_no",$this->ref_no,PDO::PARAM_STR);
            $command->bindParam(":estimate_date",$this->estimate_date,PDO::PARAM_STR);
            $command->bindParam(":property_id",$this->property_id,PDO::PARAM_STR);
            $command->bindParam(":amount",$this->amount,PDO::PARAM_STR);
            $command->bindParam(":status",$this->status_search,PDO::PARAM_STR);
            $count=$command->queryScalar();
    
            $sql="SELECT * FROM sp_sales_estimate_search(:enquiry_id,:ref_no,:estimate_date,:property_id,:amount,:status)";
    
            $dataProvider=new CSqlDataProvider($sql, array(
            'params' => array(':enquiry_id' => $this->enquiry_id,':ref_no' => $this->ref_no,':estimate_date' => $this->estimate_date,':property_id' => $this->property_id,':amount' => $this->amount,':status' => $this->status_search),
            'totalItemCount'=>$count,
            'db'=>Yii::app()->db,
            'sort'=>array(
            'attributes'=>array(
                 'ref_no','estimate_date','property_id','amount','status_search'
            ),
            ),
            'pagination'=>array(
            'pageSize'=>10,
            ),
            ));
            return $dataProvider;
        }
    

    View

    <?php
    $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'estimate-grid',
        'itemsCssClass'=>'table table-bordered table-condensed table-hover table-striped dataTable',
        'filter'=>$model,
        'dataProvider'=>$model->search(),
        'enablePagination' => true,
            'enableHistory'=>true,
        'pagerCssClass'=>'dataTables_paginate paging_bootstrap table-pagination',
        'pager' => array('header'=>'','htmlOptions'=>array('class'=>'pagination')),
        'columns' => array(
    
                            array(name=>'ref_no','value'=>'$data["ref_no"]','header'=>'Ref No','htmlOptions'=>array('style'=>'text-align:right;width:5%')),
                array(name=>'estimate_date','value'=>'Yii::app()->dateFormatter->format("dd/MM/yyyy",$data["estimate_date"])','header'=>'Date'),
    
                ),
                'htmlOptions'=>array('class'=>'grid-view table-responsive'),
    ))
    ?>