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
Related
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....
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,
) );
}
}
The following Criteria works fine:
$criteria = new CDbCriteria(array(
'condition' => 'Id_menu = 1 ',
'select' => 'name',
'limit' => 5,
));
$dp1 = new CActiveDataProvider('post', array(
'criteria' => $criteria
));
However specifying Criteria directly on a model does not - it has no effect:
$criteria = new CDbCriteria(array(
'condition' => 'Id_menu = 1 ',
'select' => 'name',
'limit' => 5,
));
$dp1 = new CActiveDataProvider(Mdlfood::model()->find($criteria),array(),));
The following also does not work:
$criteria = new CDbCriteria(array(
'condition' => 'Id_menu = 1 ',
'select' => 'name',
'limit' => 5,
));
$model1 = new Mdlfood;
$model1->findAll($criteria);
$dp1 = new CActiveDataProvider($model1,array(),));
Can anyone explain why I cannot declare this configuration directly?
Added mdlfood
class Mdlfood extends CActiveRecord{
public function tableName()
{
return 'tblfood';
}
public function rules()
{
return array(
array('name, Url_picture, Price, Aboute, Id_foodType, Id_menu', 'required'),
array('name', 'length', 'max'=>100),
array('Url_picture, Aboute', 'length', 'max'=>2048),
array('Price, Id_foodType, Id_menu', 'length', 'max'=>20),
array('Id, name, Url_picture, Price, Aboute, Id_foodType, Id_menu', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array(
'idMenu' => array(self::BELONGS_TO, 'Tblrestmenu', 'Id_menu'),
'idFoodType' => array(self::BELONGS_TO, 'Tblfoodtype', 'Id_foodType'),
);
}
public function attributeLabels()
{
return array(
'Id' => 'ID',
'name' => 'Name',
'Url_picture' => 'Url Picture',
'Price' => 'Price',
'Aboute' => 'Aboute',
'Id_foodType' => 'Id Food Type',
'Id_menu' => 'Id Menu',
);
}
public function search()
{
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('Id',$this->Id,true);
$criteria->compare('name',$this->name,true);
$criteria->compare('Url_picture',$this->Url_picture,true);
$criteria->compare('Price',$this->Price,true);
$criteria->compare('Aboute',$this->Aboute,true);
$criteria->compare('Id_foodType',$this->Id_foodType,true);
$criteria->compare('Id_menu',$this->Id_menu,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
But, I think this is not related to Model, because this Criteria work properly in the ActiveDataProvider.
Please try like this,
extra closing brackets, find()-> not in proper format
$criteria=new CDbCriteria(array(
'condition'=>'Id_menu = 1 ',
'select'=>'name',
'limit'=>5,
));
$dp1 = new CActiveDataProvider(Mdlfood::model()->find($criteria),array()); // extra closing brackets
Second way,
$criteria=new CDbCriteria(array(
'condition'=>'Id_menu = 1 ',
'select'=>'name',
'limit'=>5,
));
$model1 = new Mdlfood;
$model1->findAll($criteria);
$dp1 = new CActiveDataProvider($model1,array());
I am trying to show my data in gridview and in process have written the following query which I am trying to write using CDBCriteria,
//query
SELECT user. * , jobs. * , COUNT( jobs.user_id )
FROM user
INNER JOIN jobs ON user.id = jobs.user_id
GROUP BY user.id
I have tried the following thing:
$criteria = new CDbCriteria;
$criteria->select ='user.*,jobs.*';
$criteria->select ='COUNT(jobs.user_id)';
$criteria->select ='user';
$criteria->join ='INNER JOIN jobs ON user.id = jobs.user_id';
$criteria->group ='user.id';
return new CActiveDataProvider('manageemployers', array(
'criteria'=>$criteria,
my view have the following code.
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' =>$model->search(),
// 'filter' => $model,
'columns' => array(
array(
'name' => ' Employer ID',
'type' => 'raw',
'value' => 'CHtml::encode($data->id)',
'htmlOptions' => array('style'=>'width:90px;','class'=>'zzz'),
// 'filter'=>'false' /* for hiding filter boxes */
[...]
?>
My contoller
public function actionManageEmployers() {
$user_id = Yii::app()->session['user_id'];
if (Yii::app()->user->getId() === null)
$this->redirect(array('site/login'));
$model = new ManageEmployers();
$model->user_id = $user_id;
$this->render('manageemployers', array('model' => $model,
));
}
But its not working. Please help me on this one. Thanks!
If you have table relations set up, then in your User model (that refers to your user table) try writing your CDbCriteria like so:
$criteria = new CDbCriteria();
$criteria->with = array('jobs' => array('joinType' => 'STRAIGHT_JOIN'));
$criteria->together = true;
$criteria->addCondition('id = jobs.user_id');
return new CActiveDataProvider('manageemployers', array(
'criteria' => $criteria
));
EDIT
So now I'm able to understand your problem and I tried to fix it with this.
i think i had the solution, you don't need to get the data in the search() like you did it. you can use relations and other stuff to get your data.
firstly i would add a relation to your user-model like this:
'jobs' => array(self::HAS_MANY, 'Jobs', 'user_id')
after that, you can access all jobs of the user with $model->jobs
then you need to undo your changes of the search()-function. I mean that it must look like this:
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('attribute',$this->attribute);
//this is just an example ^
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
changes in the action:
$model = new ManageEmployers();
to this:
$model = new ManageEmployers("search");
changes in the view:
add something like this to your colums array:
array(
'name' => 'Total Jobs Posted',
'value' => 'count($data->jobs)',
'htmlOptions' => array('style'=>'width:90px;','class'=>'zzz'),
)
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
}