Yii Sortable Attributes - php

I have the following talbes but when I define firstname as sortable it is not working (not showing firstname as link where I can click and sort the List View). Despite that if I user username is working just fine.
| User
| - userid
| - username
| Profile
| - userid
| - firstname
| - lastname
I have in controller:
$criteria->with=array(
'profile',
);
$criteria->addCondition('status = 1 or status = 2 or status = 3');
if($search)
$criteria->addCondition("firstname = '{$search}'");
$dataProvider=new CActiveDataProvider('YumUser', array(
'criteria' => $criteria,
'pagination'=>array(
'pageSize'=>50,
)));
In view:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'template' => '{summary} {sorter} {items} <div style="clear:both;"></div> {pager}',
'sortableAttributes'=>array(
'firstname',
),
));

This only works if the sort property of the dataProvider is set explicitly (in these cases, where you're sorting by an attribute that is in another model)
Try this
$dataProvider = new CActiveDataProvider('YumUser', array(
'criteria' => $criteria,
'sort'=>array(
'attributes'=>array(
'firstname'=>array(
'asc'=>'firstname',
'desc'=>'firstname DESC',
),
),
),
'pagination'=>array(
'pageSize'=>50,
)));

Related

Pagination for CSqlDataProvider in CGridView

I wanna display the retrieved data i.e CSqlDataProvider in CGridView through Pagination..
and I retrieved data from table through CSqlDataProvider and shown in CGridView
but the problem is at Pagination part..
Firstly look at my Controller code..
$count = Yii::app()->db->createCommand('SELECT COUNT(familyid) FROM (' . $sql2 . ') as count_alias')->queryScalar();
$rawData = Yii::app()->db->createCommand($sql2);
$dataProviderRegSum = new CSqlDataProvider($rawData, array(
'keyField' => 'familyid',
// 'pagination'=>false,
/* 'pagination' => array(
'pageSize' => Yii::app()->user->getState( 'pageSize', Yii::app()->params[ 'defaultPageSize' ] ),PaymentTxnDate
), */
'pagination'=>array('pageSize'=>20),
'totalItemCount' => $count,
'sort'=>array(
//'defaultOrder' => 'CreatedDate desc',
'defaultOrder' => 'PaymentTxnDate asc',
'attributes'=>array(
'RegistrationID'=>array(
'asc'=>'familyid',
'desc'=>'familyid DESC',
),
'FirstName'=>array(
'asc'=>'firstname',
'desc'=>'firstname DESC',
),
'LastName'=>array(
'asc'=>'lastname',
'desc'=>'lastname DESC',
),
'CreatedDate'=>array(
'asc'=>'CreatedDate',
'desc'=>'CreatedDate DESC',
),
'EmailedDate'=>array(
'asc'=>'EamiledDate',
'desc'=>'EamiledDate DESC',
),
),),
));
and my view is as
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'family-record-grid',
'dataProvider'=>$arr[1],
'enableSorting' => false,
//'enablePagination'=>false,
'itemsCssClass'=>'tabchin',
'columns'=>
array(),
));
here $sql2 is my query....
$arr[1] is dataprovider that has sent from controller to view as array...
I have totlaItemCount value
and pageSize is 20
All is right but in the output the first 20 records are displaying its ok
but pagelinks i.e pagenumbers for pagination is not displaying at the bottom..
its been two days stuck for this error ..still didnt get solution
please help me anyone thanks for reading the stuff...
help needed..

yii tbgridview. showing only paritcular row data

I have a inventory table. Now, using this table, I want to show the data on Pharmacy module in a Tbgridview such that it only displays those inventories from the Pharmacy store. i.e. row with category_id=2.
How, can I do this on the drug's index page. This is the code for index page of Pharmacy.
<?php
$this->menu=array(
array('label'=>'Add Drug', 'icon'=>'icon-plus', 'url'=>Yii::app()->controller->createUrl('create'), 'linkOptions'=>array()),
array('label'=>'List Drugs', 'icon'=>'icon-th-list', 'url'=>Yii::app()->controller->createUrl('index'),'active'=>true, 'linkOptions'=>array()),
array('label'=>'List Orders', 'icon'=>'icon-th-list', 'url'=>Yii::app()->controller->createUrl('PhOrder/index'), 'linkOptions'=>array()),
array('label'=>'List Distributors', 'icon'=>'icon-th-list', 'url'=>Yii::app()->controller->createUrl('PhDistributor/index'), 'linkOptions'=>array()),
);
?>
<div>
<?php ?>
<?php
$model= new Inventory;
$this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'ph-drug-grid',
'dataProvider'=>$model->search(),
'type'=>'bordered condensed',
'template'=>'{summary}{pager}{items}{pager}',
'columns'=>array(
array(
'header'=>'S. N. ',
'value'=>'$data->inventory_code',
'type'=>'raw',
),
'description',
'manufacturer',
'minimum_order_level',
'store_id',
'stock_quantity',
array(
'header'=>'<a>Actions</a>',
'class'=>'bootstrap.widgets.TbButtonColumn',
'template' => '{dispose}',
'buttons' => array(
'dispose' => array(
'label'=>'Dispose',
'options'=>array(
'class'=>'btn btn-small',
'onclick'=>'return confirm("Are you sure, you want to dispose this drug?")'
),
)
),
'htmlOptions'=>array('nowrap'=>'nowrap'),
)
),
)); ?>
</div>
set attributes of your model, then if they are filtered in search() function of model Inventory, then result will get filtered
$model= new Inventory;
$model->category_id = 2;
you can also use CSqlDataProvider in your controller
$sql='select * from inventory where category_id=2';
$count=Yii::app()->db->createCommand('select count(*) from inventory where category_id=2')->queryScalar();
$dataProvider = new CSqlDataProvider($sql, array(
'totalItemCount'=>$count,
'keyField' => 'id',
'pagination'=>array(
'pageSize'=>10,
),
));
more info

Yii Framework : Join table (or other SQL) in data provider?

Background
I have used Gii Crud Generator with my "Category" model, and I want to modify the admin form.
I look inside "protected/views/Category/admin.php,
I found the table is render by a widget('zii.widgets.grid.CGridView'),
and it using a data Provider for it's data.
I suppose I can find some where to input the SQL query in the data Provider, but I don't understand about how's it works.
these is the code In the Model->relations(), but I don't know what to do next.
public function relations(){
return array(
'cateLang' => array(self::HAS_MANY, 'CategoryLang', 'cate_id')
);
}
where the data provider is generated :
public function search(){
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('status',$this->status,true);
$criteria->compare('createDate',$this->createDate,true);
$criteria->compare('updateDate',$this->updateDate,true);
$criteria->compare('remark',$this->remark,true);
return new CActiveDataProvider($this->with('cateLang'), array(
'criteria'=>$criteria,
));
}
Target
I want to add two more columns at the table of "protected/views/Category/admin.php,
which will show French Title & English Title of the row.
To get data in SQL, it will be :
SELECT
cate.id,
lang1.name as "FrenchTitle",
lang2.name as "EnglishTitle",
cate.updateDate,
cate.createDate,
cate.remark
FROM `category` cate
LEFT JOIN `categorylang` lang1
ON `lang1`.`cate_id` = `cate`.id
AND `lang1`.`lang_id`= 1
LEFT JOIN `categorylang` lang2
ON `lang2`.`cate_id` = `cate`.id
AND `lang2`.`lang_id`= 2
WHERE cate.status = 'live'
If I can done with data Provider, the CGridView parameter may be like this :
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'category-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'FrenchTitle',
'EnglishTitle',
'createDate',
'updateDate',
'remark',
array(
'class'=>'CButtonColumn',
),
),
));
You could try the following:
public function search(){
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('status',$this->status,true);
$criteria->compare('createDate',$this->createDate,true);
$criteria->compare('updateDate',$this->updateDate,true);
$criteria->compare('remark',$this->remark,true);
$criteria->with = array('cateLang' => array(
'condition' => 'cateLang.id = 1 OR cateLang.id = 2',
'order' => 'cateLang.id ASC'
));
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'category-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
array(
'name' => 'FrenchTitle'
'value' => '(isset($data->cateLang[0])) ? $data->cateLang[0]->name : "no Title"',
),
array(
'name' => 'EnglishTitle'
'value' => '(isset($data->cateLang[1])) ? $data->cateLang[1]->name : "no Title"',
),
'createDate',
'updateDate',
'remark',
array(
'class'=>'CButtonColumn',
),
),
));
In the search I specify that I want only cateLang object with the id 1 or 2 and then in the cgridview I display a relational object.

Yii TbGridView set filter dropdown html attributes

I am unable to figure out how to set the filter dropdown's id attribute.
Here is the view code that defines the widget.
$this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'view-grid',
'enableHistory'=>true,
'dataProvider'=>$model->search(),
'summaryText'=>"",
'filter'=>$model,
'columns'=>array(
array('name'=>'v_id','htmlOptions'=>array('style'=>'width: 60px')),
array('name'=>'v_parent_view','htmlOptions'=>array('style'=>'width: 20%'),'value'=>'$data->vParent->v_name'),
array('name'=>'v_name','type'=>'raw','value'=>function($data,$row) {
if(isset($data->vLatestVersion[0]->vv_id) && $data->vLatestVersion[0]->vv_id) {
return CHtml::link($data->v_name,array("viewVersion/update","id"=>$data->vLatestVersion[0]->vv_id));
} else {
return CHtml::link($data->v_name,array("viewVersion/create","vid"=>$data->v_id));
}
}),
array('name'=>'v_date_modified', 'type'=>'raw', 'htmlOptions'=>array('style'=>'width: 110px'), 'value'=>function($data, $row) {
return $data->modified_since . '<br/><span class="timeago">'.$data->vUserModified->username.'</span>';
}),
array(
'name'=>'v_status',
'value'=>'$data->getStatusName()',
'filter'=>Yii::app()->params['globalDmpLookups']['generalStatus'],
'filterHtmlOptions'=>array('id'=>'sel_status'),
'htmlOptions'=>array('style'=>'width: 60px'),
)
),
));
The line of code near the end that has 'filterHtmlOptions' is what I tried, but it is not working. Any help would be greatly appreciated.
The rendered html of my dropdown should look like this:
<select id="sel_status" name="View[v_status]">...</select>
But sadly it looks like this:
<select name="View[v_status]">...</select>
I found that I was trying to use a dropdown list incorrectly in my list filter for status = Active/Disabled. Doing this the Yii way is a simple matter of setting up the model like this...
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->with = array('ggUserModified'); // specify relationship
$criteria->compare('gg_id',$this->gg_id);
$criteria->compare('gg_class',$this->gg_class,true);
$criteria->compare('gg_name',$this->gg_name,true);
$criteria->compare('gg_title',$this->gg_title,true);
$criteria->compare('gg_description',$this->gg_description,true);
$criteria->compare('gg_date_modified',$this->gg_date_modified,true);
$criteria->compare('gg_status',$this->gg_status);
$criteria->compare('User.username',$this->gg_user_modified_rel,true); // use table alias name and specify relationship field property
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'gg_user_modified_rel' => array( // specify relationship field property
'asc'=>'User.username', // specify table alias name
'desc'=>'User.username DESC', // specify table alias name
),
'*',
),
),
));
}
... and setting up the view like this...
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'gallery-group-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'gg_id',
'gg_name',
array(
'name'=>'gg_user_modified_rel', // specify relationship field property
'value'=>'$data->ggUserModified->username' // specify relationship name
),
'gg_date_modified',
array(
'name'=>'gg_status',
'value'=>'$data->getStatusName()',
'filter'=>Yii::app()->params['globalDmpLookups']['generalStatus'],
'htmlOptions'=>array('style'=>'width: 120px'),
),
array('header'=>'Item Count','value'=>'$data->ggChildCount'),
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
),
),)); ?>
filterHtmlOptions is the HTML options for the table row element, not for the input.
what is the value of Yii::app()->params['globalDmpLookups']['generalStatus']?
if you want to set the filter dropdown's id attribute, set the HTML attributes on the dropdown.
for example :
// ..
'columns'=>array(
array(
'name' => 'majorId',
'filter' => CHtml::activeDropDownList($model, 'majorId', CHtml::listData(
Major::model()->findAll(), "id", "name"),
array(
'empty' => '=== Pilih Jurusan ===',
'id' => 'YOUR_ID'
)
),
),
),
// ..

Yii join two models

I've got two mysql tables:
1. prices
2. details
Table Prices
ID | DATE | CODE | PRICE
1 |2014-01-01 | AAA1 | 90.123
2 |2014-01-01 | AAB1 | 50.113
3 |2014-01-01 | AAC1 | 48.621
4 |2014-01-02 | AAA1 | 91.123
5 |2014-01-02 | AAB1 | 51.113
6 |2014-01-02 | AAC1 | 41.621
Table Details
CODE | NAME | DESCRIPTION
AAA1 | andria | A very good...
AAB1 | anasta | A very good...
AAC1 | simple | A very good...
Models:
Prices
public function relations(){
return array(
'code' => array(self::BELONGS_TO, 'details', 'code'),
);
}
Details
public function relations(){
return array(
'code' => array(self::HAS_MANY, 'prices', 'code'),
);
}
Here's the SQL code I want to execute:
SELECT * FROM prices a
JOIN (SELECT * FROM details) b
WHERE a.DATE=(SELECT MAX(DATE) FROM prices) AND a.code = b.code
My controllers:
$prices=new CActiveDataProvider('prices', array(
'criteria'=>new CDbCriteria (array(
'select'=>'code,date,close',
'condition'=>'date=(SELECT MAX(date) FROM prices)'
)),
));
$this->render('index',array(
'prices'=>$prices
));
index.php:
<?php
$this->widget('bootstrap.widgets.TbGridView', array(
'dataProvider'=>$prices,
'template'=>"{items}",
'enablePagination' => false,
'columns'=>array(
array('name'=>'code', 'header'=>'Code'),
array('name'=>'name', 'header'=>'Name'),
array('name'=>'close', 'header'=>'Close'),
),
)); ?>
With this controller commands, I can get the data from table prices, but I can't get the data from table details.
Take a look at the with part of the criteria: http://www.yiiframework.com/doc/api/1.1/CDbCriteria#with-detail
I think your code should look something like this (untested):
$prices=new CActiveDataProvider('prices', array(
'criteria'=>new CDbCriteria (array(
'select'=>'code,date,close',
'with' => 'details',
'condition'=>'date=(SELECT MAX(date) FROM prices)'
)),
));
you already have reliation in Model. So do query like this in your controller
$prices=new CActiveDataProvider('prices', array(
'criteria'=>new CDbCriteria (array(
'condition'=>'date=(SELECT MAX(date) FROM prices)'
)),
));
In View just use
$data->code->name
$data->code->description
Here's what I change:
index.php
<?php
$this->widget('bootstrap.widgets.TbGridView', array(
'dataProvider'=>$prices,
'template'=>"{items}",
'enablePagination' => false,
'columns'=>array(
array('name'=>'code', 'header'=>'Code'),
array('name'=>'name', 'header'=>'Name', 'value'=>'$data->details->name'),
array('name'=>'close', 'header'=>'Close'),
),
)); ?>
Models Prices
public function relations(){
return array(
'details' => array(self::BELONGS_TO, 'Stocks_details', 'code'),
);
}

Categories