suppose i have 10 row of data in DB and when i use search function in search model
suppose ill get 10 results when i call below function
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->getCount() --- in view i get 10
and if i set pageSize=5
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => array('pageSize' => 5),
]);
$dataProvider->getCount() --- in view i get 5
ill get count 5 on each page.
is there anyway to get total count on every page??
$dataProvider->getTotalCount();
Related
Hello everyone :slight_smile:
i try to show 2 gridviews of datas.
a first gridview where any of three fields is empty (so if one or two of thoses fields are empty, it should show the entry)
and a second gridview with the records that have those 3 fields filled up.
i do something wrong, because if the date field is entered, then the record goes from the first to the second gridview, event if the 2 other fields remain empty…
this is my code :
(dataprovider is the second gridview and unprocessed is the first)
if(Yii::$app->user->can('admin')){
$searchModel = new DeliverySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->orderBy(['delivery_expedition_date'=>SORT_DESC]);
}
elseif(Yii::$app->user->can('maker')){
$maker = Maker::find()->where(['user_id'=>Yii::$app->user->identity->id])->one();
$searchModel = new DeliverySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->andWhere(['maker_id'=>$maker->id]);
//$dataProvider->query->where(['and',['not',['delivery_trackcode' => null,'delivery_carrier' => null,'delivery_expedition_date' => null]]]);
$dataProvider->query->andWhere(['not', ['delivery_trackcode' => null]]);
$dataProvider->query->andWhere(['not', ['delivery_carrier' => null]]);
$dataProvider->query->andWhere(['not', ['delivery_expedition_date' => null]]);
$dataProvider->query->orderBy(['delivery_expedition_date'=>SORT_DESC]);
$unProcessed = $searchModel->search(Yii::$app->request->queryParams);
$unProcessed->query->andWhere(['maker_id'=>$maker->id]);
$unProcessed->query->andWhere(['or',['delivery_trackcode'=> NULL],['delivery_carrier'=> NULL],['delivery_expedition_date'=> NULL]]);
$unProcessed->query->orderBy(['created_at'=>SORT_DESC]);
}
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'unProcessed' => $unProcessed,
]);
Any help would be very much appreciated :slight_smile:
kind regards
So, i finally found the solution. the expedition date need to be check if NULL or not, but the carrier and trackcode need to be checked for empty ('') and not for NULL
->query->andWhere(['not', ['delivery_trackcode' => null]]);
->query->andWhere(['not', ['delivery_carrier' => null]]);
Am trying to return users with paycheck and loggedout at a certain time and return 10 items per each page but it fails to work
I have tried with the following code
$filters = $arr["filters"];
$timev = [strtotime($filters["duration_from"]), strtotime($filters["duration_to"])]
$query = Users::find()
->leftJoin('tbl_truck_history', 'tbl_paychecks.user_id = users.id')
->leftJoin('tbl_security_login', 'tbl_security_login.user_id = users.id')
->where(["users.department"=>3])
->andWhere(['between', 'tbl_security_login.time_out', min($timev), max($timev)]);
$data = new ActiveDataProvider([
'query' => $query,
'pagination' =>[
'pageSize' => 10, //here per_page is
'page' => $arr['page']
],
]);
return ["data" => $data->getModels(),"totalRecords" => (int)$query->count()]
When i check on $data->getModels() it returns only 3 items in the array. What am i missing out
The problem:
Your ids are not unique (which will be used as a key). Primary key will appear multiple times in your result, and it will treated as a same row.
Overcome this problem:
You will need to "group by" your records. Maybe use users.id for this.
$query = Users::find()
...
->groupBy(['users.id']);
If group by is not an option for you, you will need to specify the key for the lines
class ActiveDataProvider extends BaseDataProvider
...
/**
* #var string|callable|null the column that is used as the key of the data models.
* This can be either a column name, or a callable that returns the key value of a given data model.
*
* If this is not set, the following rules will be used to determine the keys of the data models:
*
* - If [[query]] is an [[\yii\db\ActiveQuery]] instance, the primary keys of [[\yii\db\ActiveQuery::modelClass]] will be used.
* - Otherwise, the keys of the [[models]] array will be used.
*
* #see getKeys()
*/
public $key;
in your case:
$data = new ActiveDataProvider([
'query' => $query,
'pagination' =>[
'pageSize' => 10, //here per_page is
'page' => $arr['page']
],
'key' => static function($model) {return [new unique key] }
]);
I would like to add a groupby with cacluations in yii2 ActiveDataProvider.
SO currently i have the following fields in my database
tbl_products
id, products, pts_log,pts_chum
SO i would like to group my data with the formula
pts_log * pts_chum / 100
So i have the following in my controller
ActiveDataProvider([
'query' => $query,
'sort' => ['defaultOrder' => ['(pts_log * pts_chum / 100)' => SORT_DESC]],
'pagination' => [
'pageSize' => $this->paginator['perPage']/2,
'page' => $this->paginator['page']
],
]);
But am now getting an error
undefined (pts_log * pts_chum / 100)
This works with one item key like pts_log. What do i add to make sorting work with a formulae.
You have to add an alias to the field the query is to be grouped by and use the alias in sort.
$query = (new \yii\db\Query())
->select ('count(*) as c, ((pts_log * pts_chum / 100) as calc_field')
->from('tbl_products')
->groupBy('calc_field');
$dataProvider = new \yii\data\ActiveDataProvider([
'query' => $query,
'sort' => ['defaultOrder' => ['calc_field' => SORT_DESC]],
]);
Use simple the desired aggregates instead of count(*).
In this case You getting an error undefined (pts_log * pts_chum / 100) because it is not column name and not valid expression.
You need create new variable in ActiveRecord model and and fill it with data from the calculated expression.
Then this variable name use in sort 'sort' => ['defaultOrder' => ['expr_item' => SORT_DESC]]
Look to http://webtips.krajee.com/filter-sort-summary-data-gridview-yii-2-0/ A similar option is considered here.
Or prepare $query like:
$tn = TblProdukts::tableName();
$query = TblProdukts::find()->select([
TblProdukts::tableName().'.*',
"(".$tn.".id*".$tn.".rank/100) AS expr"
]);
In Active Record class create variable $expr;
If needed add it to safe rules:
public function rules()
{
return [
[[
'expr'// expression
],
'safe'],
];
}
This question already has answers here:
LIMIT is not working in ActiveDataProvider
(2 answers)
Closed 6 years ago.
Offset is not working- is it correct? The pagination doesn't work.
$query = Event::find()
->offset(4)
->groupBy("sort_today");
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
If you want to set offset manually you need to switch off the pagination.
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => false
]);
Otherwise pagination resets offset to whatever page is current.
Yii2's index page's default data provider is like following:
$dataProvider = new ActiveDataProvider([
'query' => ModelName::find(),
]);
Now, I've got an array like $arr = [1, 2, 4, 6];
I want to add a where clause like:
WHERE parentId=1 OR parentId=2 OR parentId=4 OR parentId=6
How can I do that?
Can be done like this:
$query = ModelName::find()->where(['parentId' => $arr]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
When you pass an array to where, Yii automatically transforms it into IN condition.
So generated SQL conditional part will be WHERE parentId IN (1, 2, 4, 6);.
It's equivalent to your mentioned condition with OR.