I have a problem , i have 2 tables users and data , relation is one to one , here is code for dataProvider
$dataProvider = new CActiveDataProvider('Users', array(
'pagination'=> array(
'pageSize'=> 10
),
'criteria'=>array(
'with' => array (
'data'=>array(
'joinType'=>'JOIN')
)
),
)
));
and relations 'data' => array(self::HAS_ONE, 'Data', 'id');
and code for TbGridView
$this->widget('bootstrap.widgets.TbGridView', array(
'dataProvider'=>$dataProvider,
'template'=>"{items} {pager}",
'itemsCssClass'=>'table table-striped table-bordered table-condensed tableNews',
'columns'=>array(
array('name'=>'id', 'header'=>'#'),
array('name'=>'name'),
array('name'=>'email'),
array('name'=>'data.id'),
array('name'=>'data.investment_amount')
));
for second table i have to put data.fildname , else is not working , and fildnames of second table is not clickable
what can be the problem , thank you
return new CActiveDataProvider("Users", array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>$defaultOrder,
'attributes'=>array(
'id'=>array(
'asc'=>'t.id',
'desc'=>'t.id DESC',
),
'data.investment_amount'=>array(
'asc'=>'data.investment_amount',
'desc'=>'data.investment_amount DESC',
),
but if you use sort you need to define all names inside attributes array
Related
i want to create a table like this Yii Booster Gridview table
here is my code in controller:
$rawData=Jobspecs::model()->with('customer')->findAll();
$gridDataProvider=new CArrayDataProvider($rawData, array(
'id'=>'user',
'sort'=>array(
'attributes'=>array(
'id', 'customer',
),
),
));
$gridColumns = array(
array('name'=>'id', 'header'=>'Js No.', 'htmlOptions'=>array('style'=>'width: 60px')),
array('name'=>'WHAT TO PUT HERE TO SHOW CUSTOMER NAME', 'header'=>'Customer Name'),
array(
'htmlOptions' => array('nowrap'=>'nowrap'),
'class'=>'booster.widgets.TbButtonColumn',
'viewButtonUrl'=>null,
)
);
in my model sapcustomers:
return array(
'customer'=>array(self::BELONGS_TO, 'Sapcustomers', 'customer'),
);
jobspecs model
return array(
'cardname'=>array( self::HAS_MANY, 'Jobspecs', 'customer' ),
);
my view
<?php
$this->widget(
'booster.widgets.TbGridView',
array(
'type' => 'bordered',
'dataProvider' => $gridDataProvider,
'template' => "{items}",
'columns' => $gridColumns,
)
);
?>
as you can see i joined sapcustomers and jobspecs table. my question is what code i need to put on the $gridcolums to show the customer name data from the table sapcustomer. thanks for the help
I think this should work for you:
$gridColumns = array(
[...]
array(
'name'=>'customer',
'value'=>'$data->customer->name',
'header'=>'Customer Name'
),
);
Explanation
The $data stands for the Jobspecs-model from the actual row. Then you access the related customer and his name.
In my ClistView i'm trying to set a default sort and define my sortable attributes in my view. I've gotten this far
in my actionIndex()
$criteria=new CDbCriteria(array(
'condition'=>'make_code!="00"',
));
$dataProvider=new CActiveDataProvider('StoreNew', array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'_make DESC',
'attributes'=>array(
'_make'=>array(
'asc'=>'_make',
'desc'=>'_make DESC',
),
'*', //if attributes contains a star ('*') element, the name will also be used to match against all model attributes.
)
),
));
in my model
public function relations()
{
return array(
'_state' => array(self::BELONGS_TO, 'State', 'state'),
'_make' => array(self::BELONGS_TO, 'pMake', '',
'foreignKey' => array('make_code'=>'make_code')),
);
}
and in my view
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'sortableAttributes'=>array(
'_make' => 'Make',
'store',
'state',
),
));
im getting this error
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column '_make' in 'order clause'. The SQL statement executed was: SELECT * FROM `store_new` `t` WHERE make_code!="00" ORDER BY _make DESC LIMIT 10
how to i sort table pMake.make?
try this in your actionIndex()
$criteria=new CDbCriteria(array(
'with' => array('_make'), // join the _make relation you defined in your model into this query
'condition'=>'t.make_code!="00"',
));
then
$dataProvider=new CActiveDataProvider('StoreNew', array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'make'=>array(
'asc'=>'_make.make',
'desc'=>'_make.make DESC',
),
'*', //if attributes contains a star ('*') element, the name will also be used to match against all model attributes.
)
),
));
then in your view
'sortableAttributes'=>array(
'make' => 'Make', //you can call "make" base on 'attributes'=>array('make'=>array())
'store',
'state',
),
note tested. hope it works.
I am trying to set the pageSize property of pager in CGridView but in vain. By the way currently there are total of 2 items, i want to display only 1 on 1 page. Thanks.
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'pager' => array(
'pageSize' => 1,
),
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'username',
'email',
'pass',
'type',
'date_entered',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
remove the following from the view
'pager' => array(
'pageSize' => 1,
),
inside the dataprovider array in your model search method add this code
$dataProvider = new CActiveDataProvider('your_model', array(
'pagination'=>array(
'pageSize'=>your_page_size,
),
'criteria'=>$criteria,
));
I'm using CSqlDataProvider to construct CGridview I cannot use CActiveRecord because the result set is huge and throwing memory errors. The columns need to be sortable. How should I achieve this?
Sample sql
$orders_query_raw = 'select o.order_id, o.customer_name, o.customer_email, o.customer_advertiser, o.payment_method, o.created, o.last_updated, o.currency, o.currency_value, o.status, o.blinking, s.name, ot.text order_total, o.customer_id, op.product_id, o.phonebooking
from `order` o, `order_total` ot, `order_status` s , order_product op
where o.order_id = op.order_id and o.status = s.order_status_id and ot.order_id = o.order_id and s.language_id = '1' and ot.class = 'ot_total' group by o.order_id'
sql dataprovider
$dataProvider = new CSqlDataProvider($orders_query_raw, array(
'totalItemCount'=>$count, // get from a count query
'pagination'=>array(
'pageSize'=>50,
),
));
And gridview
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'id'=>'order-grid',
'columns' => array(
array(
'header'=>'Order ID',
'value'=>array($this, 'gridOrderId'),
'type'=>'raw',
),
array(
'header'=>Yii::t('order', 'Customers'),
'name'=>'customer.fullName',
'value'=>'CHtml::link($data[\'customer_name\'], \'mailto:\'.$data[\'customer_email\'])',
'type'=>'raw',
),
array(
'header'=>Yii::t('order', 'Order total'),
'value'=>'strip_tags($data[\'order_total\'])',
'type'=>'raw',
'htmlOptions'=>array(
'style'=>'text-align:right;',
),
),
array(
'header' => Yii::t('order', 'Date Purchased'),
'name' => 'created',
),
array(
'header'=> Yii::t('order', 'Last modify date'),
'value'=>array($this, 'gridLastModified'),
),
array(
'header' => Yii::t('order', 'Status changed by'),
'value' => array($this, 'gridLastModifiedUserFirstName'),
),
array(
'header' => Yii::t('provider', 'Provider\'s code'),
'value' => array($this, 'gridProviderCode'),
'type' => 'raw',
'htmlOptions'=>array(
'class'=>'nobr',
),
),
array(
'header' => Yii::t('order', 'Follow up'),
'value' => array($this, 'gridFollowUp'),
'type' => 'raw',
),
array(
'header' => Yii::t('order', 'Order status'),
'value' => '$data[\'name\']',
),
array(
'class'=>'CButtonColumn',
'template'=>'{update}',
'header'=>'Action',
'buttons'=>array(
'update'=>array(
'url'=>'Yii::app()->createUrl(\'order/update\', array(\'order_id\'=>$data[\'order_id\']))',
),
),
),
),
));
Thanks
To enable sorting (by clicking on header of a column) in the grid-view with data provider as CSqlDataProvider, you'll need minimally 2 things:
Have to define the CSort object for the data provider, with the attributes that would be sortable.
Have to define the name of the column but only in case you are specifying the columns property of the grid-view, otherwise if the columns property is left blank, whatever attributes are mentioned in the CSort object will be sortable.
That said, the other answer should work in cases when the sql is simple, and comes from 1 table, but in your case, where the sql is a little complicated i.e data comes from multiple tables, the solution will change slightly.
In such cases you'll have to account for conflicting column names(if any), and proper specification of CSort's attributes array.
Examples:
No conflicting column names in any of the tables (same as the other answer):
$dataProvider=new CSqlDataProvider($sql, array(
'totalItemCount'=>$count,
'sort'=>array(
'attributes'=>array(
'order_id, order_total' // csv of sortable column names
)
)
));
Then in your grid:
array(
'header'=>Yii::t('order', 'Order total'),
'name'=>'order_total',// to make header clickable to sort
'value'=>'strip_tags($data[\'order_total\'])',
'type'=>'raw',
'htmlOptions'=>array(
'style'=>'text-align:right;',
),
),
Conflicting column names:
First, give all conflicting names aliases in your sql.
Second, specify those aliases as the sortable attributes in the CSort object:
'attributes'=>array(
'some_alias, some_other_alias'
)
Specify the name for the column in columns:
array(
'header'=>'Foo',
'name'=>'some_alias',
'value'=>'$data[\'some_alias\']' // this is actually redundant in this
// case, because the name will itself pick up the value, and we don't
// need to specify value explicitly if we are not applying any function to it
)
Note that sorting by url calling is enabled by just specifying the sort object, no need to use name, unless you want click to sort headers.
Try as below
$sort = new CSort();
$sort->defaultOrder = 'order_id'; // for initial order
$sort->attributes = array(
'created'
);
$dataProvider = new CSqlDataProvider($orders_query_raw, array(
'totalItemCount'=>$count, // get from a count query
'pagination'=>array(
'pageSize'=>50,
),
'sort'=>$sort
));
I'm trying to load a cGridView with the results of a query between two tables (charity and votes).
Trying to show the number of votes in the vote table for a charity. The vote table has a FK to the charity table.
I could do this in SQL with a left join, but cGridView requires a CActiveDataProvider object to display the data and I am unsure how I can join the two tables to return a result that not only counts, but also doesn't show any results that are equal to 0 and ordered by the votes.
I currently am doing:
in the Vote Model:
public function relations()
{
return array(
'voteCount'=>array(self::STAT, 'Vote', 'charity_id'),
);
}
charity_id being the FK for the charity table.
Then to build the CGridView widget:
$criteria=new CDbCriteria(array(
'with' => 'voteCount',
));
$dataProvider=new CActiveDataProvider('Charity', array(
'pagination' => false,
'criteria' => $criteria,
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'ajaxUpdate'=>true,
'columns'=>array(
'Name',
array(
'name'=>'vote.voteCount',
'value'=>'CHtml::encode($data->voteCount)',
),
),
));
Right now it's returning multiple results and I can't seem to figure out how to sort and add a where clause as well.
Any help?
Try add to Charity model
public function relations()
{
return array(
'vote'=>array(self::HAS_ONE, 'Vote', 'charity_id'),
);
}
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'ajaxUpdate'=>true,
'columns'=>array(
'Name',
array(
'name'=>'Vote Count',
'value'=>'CHtml::encode($data->vote->voteCount)',
),
),
));
You need some minor changes in
Vote.php (Vote Model)
public function relations()
{
return array(
'voteCount'=>array(self::STAT, 'Vote', 'charity_id'),
);
}
Add a field in attributeLabels()
'voteCount'=>'Votes',
and In CGridView add the 'Votes' column
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'ajaxUpdate'=>true,
'columns'=>array(
'Name',
'Votes',
),
));