Yii Framework Forum: Show Data In Cgridview Using Custom Sql Query - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Show Data In Cgridview Using Custom Sql Query

#1 User is offline   Mizanur 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 11-April 14
  • Location:Bangladesh

Posted 26 April 2014 - 03:12 AM

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.... :(
0

#2 User is offline   chandran 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 513
  • Joined: 08-October 12
  • Location:Austria

Posted 06 May 2014 - 08:49 AM

Hi,I think u r not getting any values in the list. that is the reason for exception

Print_r($mealExpenseDetails);


in your controller

just check above
Thanks
chandran nepolean

My own extension:
http://www.yiiframew...sion/ejqueryte/

My wiki
http://www.yiiframew...checkboxcolumn/

If it work dont hesitate to click +1 button
0

#3 User is offline   sanyalex 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 19-June 13

Posted 19 February 2015 - 12:12 PM

It has to be "return $dataProvider;" instead of "return $dataProvider->getData();" in get_expense_details() method
0

#4 User is offline   Dan Din Dun 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 09-February 10

Posted 17 March 2015 - 08:16 PM

change this function to be like this:

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;
        }


instead returning list data, it should returning CDataProvider
Thanks and Kind Regards
------
DanDinDun
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users