irfan
(Mr Irfanshaukat)
May 17, 2011, 1:11pm
1
I have two tables "Orders" and "Customers", How can I show data using dataGrid Widget?
I am also using "EActiveDataProvider" extension and doing
$dataProvider=new EActiveDataProvider('Orders', array(
// 'scopes'=>array('charged'),
'criteria'=>array(
//'condition'=>'status=1 AND tags LIKE :tags',
//'params'=>array(':tags'=>$_GET['tags']),
'with'=>array('customers'),
),
'pagination'=>array(
'pageSize'=>20,
),
));
$dataProvider->joinAll = true; // optional if you want to also join the *_MANY relations
$dataProvider->getData(); // will return a list of customers objects
return $dataProvider;
and in view If I want to show Email from customers table, it shows error
<?php $this->widget(‘zii.widgets.grid.CGridView’, array(
'id'=>'orders-grid',
'dataProvider'=>$model->fetchData(),
'filter'=>$model,
'columns'=>array(
'ID',
'FirstNameB',
'LastNameB',
'Email',
Any suggestion will be highly appreciated
Could you give us the error that’s appearing, please?
The reason for the error is most likely because ‘Email’ is not a column in your orders table.
I’m not familiar with the EActiveDataProvider extension.
In the ‘columns’ array of your CGridView, try this:
'columns'=>array(
....
array(
'name'=>'email',
'value'=>'$data->customer->email',
)
),
in place of the ‘Email’ entry you have in your code sample.
irfan
(Mr Irfanshaukat)
June 6, 2011, 7:23am
3
georgebuckingham:
Could you give us the error that’s appearing, please?
The reason for the error is most likely because ‘Email’ is not a column in your orders table.
I’m not familiar with the EActiveDataProvider extension.
In the ‘columns’ array of your CGridView, try this:
'columns'=>array(
....
array(
'name'=>'email',
'value'=>'$data->customer->email',
)
),
in place of the ‘Email’ entry you have in your code sample.
Thanks but you are too late, I got solutions far earlier.
Can you post the solution here, so other people can learn?
irfan
(Mr Irfanshaukat)
June 7, 2011, 5:25am
5
OK, Here is what I did… This will fetch data from orders and customers tables.
$this->widget(‘zii.widgets.grid.CGridView’, array(
‘dataProvider’=>$model->search(),
‘filter’=>$model,
‘columns’=>array(
'ID', // display the 'ID' attribute
'Amount', // display the 'Amount' attribute
//'customers.FirstNameB', // show attribute 'FirstNameB' from 'customers' relation
//'customers.LastNameB', // show attribute 'LastNameB' from 'customers' relation
'customers.Email', // show attribute 'Email' from 'customers' relation
//'content:html', // display the 'content' attribute as purified HTML
array( // display 'create_time' using an expression
'name'=>'Time',
'value'=>'date("M j, Y", $data->Time)',
),
array(
'name'=>'CustomerID',
//'value'=>'$data->customers->FirstNameB',
'filter' => CHtml::listData(Customers::model()->few()->findAll(),'ID','FirstNameB'),
'value' => 'Customers::model()->FindByPk($data->CustomerID)->FirstNameB',
),
array( // display a column with "view", "update" and "delete" buttons
'class'=>'CButtonColumn',
),
),
));