hi,
I want to display data from database in CGridView using custom MySql query.
my code is below-
Model:
public function get_expense_details($id)
{
$count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM meal_expenses_details WHERE meal_expenses_id='.$id)->queryScalar();
$sql='SELECT * FROM meal_expenses_details WHERE meal_expenses_id='.$id;
$dataProvider=new CSqlDataProvider($sql, array(
'totalItemCount'=>$count,
'sort'=>array(
'attributes'=>array(
'product_title', 'qty', 'unit', 'price',
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
return $dataProvider->getData(); /*will return a list of arrays.*/
}
Controller:
public function actionView($id)
{
$mealExpenses = $this->loadModel($id);
$objMealExpensesDetails = new MealExpensesDetails;
$mealExpenseDetails = $objMealExpensesDetails->get_expense_details($id);
$this->render('view',array(
'meal_expenses'=>$mealExpenses,
'meal_expense_details'=>$mealExpenseDetails,
));
}
View:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'meal-expenses-details-grid',
'dataProvider'=>$meal_expense_details,
'columns'=>array(
'product_title',
'qty',
'unit',
'price',
),
));
But it throw me below error
Fatal error: Call to a member function getData() on a non-object in C:\xampp\htdocs\project_name\framework\zii\widgets\CBaseListView.php on line 111
please guys help me i just tired…