I wants to do pagination with Store Procedure yii, when i load page first time it shows me 10 records but when i click on 2nd page it shows me no record found, Here is my code
if(isset($_GET['page']) && $_GET['page']!='') {
$page = $_GET['page'];
} else {
$page = 1;
}
$SP = "CALL Dormantreport(1,'2015-01-01','2015-12-31','',".$page.",10)";
$command = Yii::app()->db->createCommand($SP);
$rawDataWithArray = $command->queryAll();
$filteredData=$filtersForm->filter($rawDataWithArray);
$model = new CArrayDataProvider($rawDataWithArray, array(
'keyField' => 'MemberID',
'totalItemCount' => 78, //count($rawDataWithArray),
'sort' => array(
'attributes' => array(
'MemberID',
),
'defaultOrder' => array(
'MemberID' => CSort::SORT_DESC,
),
),
'pagination' => array(
'pageSize' => $PageSize,
),
));
return $model;
Can anyone please tell me what i need to do now to solve this pagination issue, any help will be really appreciated,
In your model,
public function search() {
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria = new CDbCriteria;
$criteria->order = 'employeedetailsid DESC';
$criteria->compare('employeedetailsid', $this->employeedetailsid);
$criteria->compare('employeemasterid', $this->employeemasterid);
$criteria->compare('employeedetails_qualification', $this->employeedetails_qualification, true);
$criteria->compare('employeedetails_experience', $this->employeedetails_experience, true);
$criteria->compare('employeedetails_address1', $this->employeedetails_address1, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pagesize' => 25,
)
));
}
Try this....
Related
I have to check no of products quantity from other table and display it in current grid view. So i created one function in model for getting count of that column.
But my question is How can I sort that custom column (checked_kpl) in grid view.
Here is my code.
MODEL
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id, true);
$criteria->compare('purchase_order_id', $this->purchase_order_id);
$criteria->compare('product_id', $this->product_id);
$criteria->compare('unit_price', $this->unit_price, true);
$criteria->compare('qty', $this->qty, true);
$criteria->compare('cost_price', $this->cost_price, true);
$criteria->compare('customs_percent', $this->customs_percent, true);
$criteria->compare('discount_percent', $this->discount_percent, true);
$criteria->compare('notes', $this->notes, true);
$criteria->compare('created_at', $this->created_at, true);
$criteria->compare('updated_at', $this->updated_at, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
}
public function getCheckedKpl() {
$checked_kpl = 0;
if (!empty($this->purchaseOrderArrivals)) {
foreach ($this->purchaseOrderArrivals as $eachArrival) {
$checked_kpl += $eachArrival->qty;
}
}
return $checked_kpl;
}
NOTE:
- purchaseOrderArrivals is another model. I already set relation with this model.
- getCheckedKpl function is giving me count of product quantity.
VIEW - In view I put this code in gridview widget for display column.
array(
'name' => 'checked_kpl',
'value' => '$data->getCheckedKpl()',
'type' => 'raw',
'class' => 'DataColumn'
),
Any help will appreciate. thanks.
class Model extends CActiveRecord {
// Adding attribute to work with SQL query
public $checked_kpl;
public function attributeLabels(){
// Prettify column name
return array( 'checked_kpl' => 'Checked kpl' );
}
public function search() {
$criteria = new CDbCriteria;
// Count subquery like this
// SELECT COUNT(*) as checked_kpl, id FROM {{table}} GROUP BY param
// Condition like this
// ( q1.id=t.id )
$criteria->join = "LEFT JOIN (/* **HERE IS A COUNT SUBQUERY** */) as q1 ON(/* **HERE IS A CONDITION** */)";
$criteria->select = array( '*', new CDbExpression("q1.checked_kpl as checked_kpl") );
// ... your criteria here
// Adding custom sort data
$sort = new CSort();
$sort->attributes = array(
'checked_kpl' => array(
'asc' => 'q1.checked_kpl',
'desc' => 'q1.checked_kpl DESC'
)
);
return new CActiveDataProvider( $this, array(
'criteria' => $criteria,
'sort' => $sort,
) );
}
}
I am using cakephp 2.x. I am unable to set maximum limit of my record.
Please check my code:
App::uses('AppController', 'Controller');
class BroadcastsController extends AppController {
public $components = array('Paginator');
public function broadcast(){
$this->Paginator->settings = array('limit' => 10, 'order' => array('Broadcast.no_of_user' => 'DESC'), 'group' => 'Broadcast.broadcaster_id');
$popularRooms = $this->Paginator->paginate('Broadcast');
pr($popularRooms); //fetch 200 records
$this->set('popularRooms', $popularRooms);
}
}
Above pr($popularRooms); I am getting 200 records but I want first 50 records and per page showing 10 records. I was using 'maxLimit'=>50 but this code same as 'limit'=>50. Please help me.
In that case, you can use the 'extras' array in your paginator setting
$this->Paginator->settings = array('limit' => 10, 'max_record'=>50, 'order' => array('Broadcast.no_of_user' => 'DESC'), 'group' => 'Broadcast.broadcaster_id');
in your model or AppModel overiding the paginateCount function
class Broadcast extends AppModel {
function paginateCount($conditions, $recursive, $extra) {
$param = array_merge(compact('conditions', 'recursive'), $extra);
$count = $this->find('count', $param);
if (!empty($extra['max_record']) && $count > $extra['max_records']) {
$count = $extra['max_record'];
}
return $count;
}
}
By default CakePHP limits the maximum number of rows that can be fetched to 100.You can adjust it as part of the pagination options like below
public $paginate = array(
// other keys here.
'maxLimit' => 10
);
For you code set the maxLimit like this
$this->Paginator->settings = array('limit' => 10,'maxLimit'=>50,'order' => array('Broadcast.no_of_user' => 'DESC'), 'group' => 'Broadcast.broadcaster_id');
public function list_posts() {
$settings = array(
'limit' => 25, // here
'order' => array(
'Post.title' => 'asc'
)
);
$this->Paginator->settings = $this->settings;
// similar to findAll(), but fetches paged results
$data = $this->Paginator->paginate('Posts');
$this->set('data', $data);
}
I believe you don't have to have $this->Paginator->settings =.
public function list_posts() {
$settings = array(
'limit' => 25, // here
'order' => array(
'Post.title' => 'asc'
)
);
// similar to findAll(), but fetches paged results
$data = $this->Paginator->paginate('Posts');
$this->set('data', $data);
Set maxLimit first in your paginator settings before limit like this:
$this->Paginator->settings = array(
'maxLimit' => 50,
'limit' => 10,
'order' => array('Broadcast.no_of_user' => 'DESC'),
'group' => 'Broadcast.broadcaster_id'
);
Only setting one of them logically does not change anything.
I have a controller that has a display data from a database depending on the ?id=, it works correctly. However, if you do not give any value id gets error
error 400
Your request is invalid.
My code:
public function actionIndex($id)
{
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$this->pageTitle = 'Page';
$criteria = new CDbCriteria(
array(
'condition' => 'name = :Name',
'params' => array(':Name' => $id),
//if $id is not defined then error
)
);
}
$ModelPages = Pages::model()->findAll($criteria);
$this->render('index',
array(
'Model' => $ModelPages,
)
);
}
I tried this out in such a way, but it did not help.
public function actionIndex($id)
{
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$this->pageTitle = 'Page';
if(empty($id)){
$criteria = new CDbCriteria(
array(
'condition' => 'name = :Name',
'params' => array(':Name' => 'index'),
)
);
}
else {
$criteria = new CDbCriteria(
array(
'condition' => 'name = :Name',
'params' => array(':Name' => $id),
)
);
}
$ModelPages = Pages::model()->findAll($criteria);
$this->render('index',
array(
'Model' => $ModelPages,
)
);
}
Is my solution is correct (safe) when it comes to displaying the content according to the site?
You solution is correct but better use getQuery() method for fetching GET parameters and handle the error if no pages found:
public function actionIndex($id='index') //Notice the default parameter value
{
$id = Yii::app()->request->getQuery('id', 'index') //if id GET parameter does not exist $id will be 'index'
$criteria = new CDbCriteria(
array(
'condition' => 'name = :Name',
'params' => array(':Name' => $id),
)
);
$ModelPages = Pages::model()->findAll($criteria);
if (empty($ModelPages)) {
throw new CHttpExeption(404,'page not found');
}
$this->render('index',
array(
'Model' => $ModelPages,
)
);
}
Also if your action can not receive id parameter you should set default value for it (actionIndex($id='index'))
Try simply this way
public function actionIndex($id)
{
if(isset($id) && $id>0)
{
$this->pageTitle = 'Page';
$criteria = new CDbCriteria(
array(
'condition' => 'name = :Name',
'params' => array(':Name' => $id),
)
);
$ModelPages = Pages::model()->findAll($criteria);
$this->render('index',
array(
'Model' => $ModelPages,
)
);
}else
throw new CHttpException(404,'invalid request');
}
I have 2 model :
$model = new ProfileInformation('internetConection');
$modeliner = new ProfileInformation('inerConection');
I show those in 2 CGridView in yii how can show in a CGridView
Model:
public function internetConection() {
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria = new CDbcriteria();
$criteria->with = array('user');
$criteria->condition = 'serviceId=:serviceId';
$criteria->params = array(':serviceId' => '1');
$criteria->group = 't.user_Id';
$criteria->select = array('count(distinct psh_profile_information_services.profileInformationId) AS internetConectionCount');
$criteria->join = 'left join psh_profile_information_services on t.id=psh_profile_information_services.profileInformationId';
$criteria->order = 't.id';
$criteria->compare('user_Id', $this->user_Id);
$criteria->compare('isService', $this->isService, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
public function inerConection() {
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria = new CDbcriteria();
$criteria->with = array('user');
$criteria->addInCondition('serviceId', array(2, 3, 4, 5));
$criteria->group = 't.user_Id';
$criteria->select = array('count(distinct psh_profile_information_services.profileInformationId) AS inerConectionCount');
$criteria->join = 'left join psh_profile_information_services on t.id=psh_profile_information_services.profileInformationId';
$criteria->order = 't.id';
$criteria->compare('user_Id', $this->user_Id);
$criteria->compare('isService', $this->isService, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
I am using 2 CgridView now , but if i can show in a table it is very good.
eache result search have a new field : inerConectionCount and internetConectionCount.
table for internetConectionCount
table for inernetConectionCount
I want it:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'profile-information-grid1',
'dataProvider' => $dataprovider
'columns' => array(
array(
'header' => '',
'value' => '$this->grid->dataProvider->pagination->offset + $row+1', // row is zero based
),
array(
'name' => 'ProfileInformation.user.organization',
'value' => 'CHtml::encode($data->user->organization)',
),
array(
'name' => 'ProfileInformation.user.scope',
'value' => 'CHtml::encode($data->user->scope->name)',
'filter' => Scope::model()->options,
),
array(
'name' => 'id',
'value' => 'CHtml::encode($data->id)',
),
'inerConectionCount',
),
));
You can combine data from two providers, but you have to disable pagination or it will limit to 10 record in each providers
$model = new ProfileInformation('internetConection');
$modeliner = new ProfileInformation('inerConection');
$data = CMap::mergeArray( // combine two data
$model->search()->getData(),
$modeliner ->search()->getData()
);
$provider = new CArrayDataProvider( $data ); // use CArrayDataProvider instead of CActiveDataProvider
I want to show 2 tables with a right join, but the code I wrote does not work as expected. Can anyone tell me what I am doing wrong ?
view : admin.php
$this->widget('bootstrap.widgets.TbGridView', array(
'id' => 'punish-grid',
'dataProvider' => $model->searchJoin(),
'type' => 'striped bordered condensed',
'filter' => $model,
'columns' => array(
array(
'header' => 'No',
'type'=>'raw',
'htmlOptions'=>array('style'=>'width: 25px'),
'value'=>'$this->grid->dataProvider->pagination->currentPage
*$this->grid->dataProvider->pagination->pageSize + $row+1',
),
// i want to display p.kode,p.status from table status
'berlaku_punish',
'nilai',
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
),
),
));
and my model : BasePunish.php
public function relations() {
return array(
'idStatus' => array(self::BELONGS_TO, 'Status', 'id_status'),
);
}
public function searchJoin() {
$criteria = new CDbCriteria;
$criteria->select = 'p.kode,p.status,t.nilai,t.berlaku_punish';
$criteria->join= 'RIGHT JOIN status p ON (t.id_status=p.id)';
$criteria->condition = 't.id_status IS NULL';
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort'=>array(
'defaultOrder'=>'kode ASC',
),
)
);
}
I might really did not understand what you are asking but still if nothing is working for you then you can try this
array(
'header'=>'Products',
'value'=>array($model,'gridCreateUser'),
),
In this, the value will try to find the function gridCreateUser in the class of which $model is the object. In your case i guess $model is the object of the BasePunish.
So in your BasePunish.php create a function gridCreateUser() and then you can return the value which you want to display in your widget.
eg:-
In your BasePunish.php
public function gridCreateUser($data)
{
// you can access the object $data here
// do what ever you want to do
$value='return whatever you want to return';
return $value;
// this $value will be displayed there
}