yii2 dataProvider- offset in dataprovider doesn't work [duplicate] - yii2

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.

Related

Yii2 ActiveDataProvider with leftJoin in query doesnt return the pagination pagesize items

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] }
]);

Yii2 sort by with calculations in an ActiveDataProviderRecord

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'],
];
}

Yii2 best way of dealing with large data with activeDataProvider in GridView?

I have a database table of sales that has 100k records. In the future months the size will double and later triple and etc.. The table has relation with two more tables that I need data from. Finally I want to list everything in GridView with ActiveDataProvider but it takes waaay to long(3-5min). I am wondering what is the best way dealing with this situation?
My current query looks like this:
$query = Sale::find()->select([
's.*',
'u.username as userName',
'ship.name as ShipName'
])
->joinWith('user')
->joinWith('shipping')
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'defaultPageSize' => 25,
'pageSizeLimit' => 1000],
]);
As you can see its a simple query, but the data is too huge to work normally.. What can I do?

yii2 dataprovider getcount with pagination

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();

Yii2 Active record query multiple where clause from array

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.