1

invoice is a view in DB and i want to show this view information in a CGridView form
this is my action:

    public function actionInvoice()
    {
  $model= Invoice::model()->findAllBySql('select * from invoice where userid ='.Yii::app()->user->id);
  $this->render('invoice',array('model'=>$model));

}

and this is my view:

<?php var_dump($model)   ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'UserAccountnumber-grid',

'dataProvider'=>$model->search(),

'filter'=>$model,
'columns'=>array(
    'amount',
    'time',
    'status',
                )        


)    



);
?>

but when i run this code i got an error :

Fatal error: Call to a member function search() on a non-object in C:\wamp\www\mypal\protected\views\user\invoice.php on line 5

i know this error is because of i send an array of model into the view but i dont know how to figure it out and show an array of models in a CGridView
tnx

2 Answers 2

1

why findAllBySql?

may be

Invoice::model()->findAllByAttributes([
    'userid' => Yii::app()->user->id
]);

And findAllBySql and findAllByAttributes return array of ActiveRecord object. if you want find only one row use findBySql or findByAttributes

but best way:

public function actionInvoice()
{
    $dataProvider=new CActiveDataProvider('Invoice', array(
        'criteria'=>array(
            'condition' => 'userid=:userid',
            'params' => [':userid' => Yii::app()->user->id]
        ),
    );
    $this->render('invoice',array('provider'=>$dataProvider));
}

and view:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'UserAccountnumber-grid',
    'dataProvider'=>$provider,
    'columns'=>array(
         'amount',
         'time',
          'status',
    )
));
Sign up to request clarification or add additional context in comments.

Comments

1

Try using CSqlDataProvider to get the data provider, and pass it to the CGridView widget.

http://www.yiiframework.com/doc/api/1.1/CSqlDataProvider

The $model->search() autogenerated function returns a CDataProvider, but your $model, is not the Model class, is a result set of a SQL query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.