DatePicker widget filter a datetime field - yii2

Friends, how do I get my DatePicker component below (in Yii2 Framewok), filter a field of type datetime? Since in the component I can only specify the date format.
_search.php file:
<?php
echo DatePicker::widget([
'model' => $model,
'attribute' => 'start_date',
'attribute2' => 'end_date',
'language' => 'pt',
'type' => DatePicker::TYPE_RANGE,
'separator' => 'até',
'options' => [
'placeholder' => '',
],
'pluginOptions' => [
'autoclose'=>true,
'todayHighlight' => true,
'format' => 'yyyy-mm-dd',
]
]);
?>
UPDATE
public function search($params)
{
$query = Report::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'defaultOrder' => [
'created' => SORT_DESC,
]
],
'pagination' => [
'pageSize' => 100,
],
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'created' => $this->created,
'updated' => $this->updated,
'closed' => $this->closed,
'user_id' => $this->user_id,
'status_id' => $this->status_id,
'location_id' => $this->location_id,
'typeperson_id' => $this->typeperson_id,
'field_cpfcnpj' => $this->field_cpfcnpj,
]);
$query->andFilterWhere(['between', 'created', $this->start_date, $this->end_date]);
$query->andFilterWhere(['between', 'closed', $this->start_closed, $this->end_closed]);
return $dataProvider;
}

If i understand correctly, you want to submit form using the date range which should filter the records using the given range.
Looking at you search() method it looks like you have declared 2 public properties/fields in the search model with the name start_date and end_date which you are using with the DatePicker and you are trying to compare the range with the column created.
You need to do the following in order to filter the records correctly
Make sure for the following
The start_date and end_date are declared inside the safe rules for the ReportSearch model.
You need to use the \yii\db\Expression to convert the date in the column to the desired format, and use the php:date to format the given date ranges i.e start_date and end_date.
Add the following before you return the $dataProvider in the search() method
if ($this->start_date !== null && $this->end_date !== null) {
$query->andFilterWhere(
[
'BETWEEN',
new Expression(
'DATE_FORMAT(created,"%Y/%m/%d")'
),
date("Y/m/d", strtotime($this->start_date)),
date("Y/m/d", strtotime($this->end_date)),
]
);
}
Note: if you have the created column saved as timestamp then you need to wrap the field name in the existing query with FROM_UNIXTIME like below, otherwise if the column is of DATE or DATETIME the above will work.
Column type is of TIMESTAMP
if ($this->start_date !== null && $this->end_date !== null) {
$query->andFilterWhere(
[
'BETWEEN',
new Expression(
'DATE_FORMAT(FROM_UNIXTIME(created),"%Y/%m/%d")'
),
date("Y/m/d", strtotime($this->start_date)),
date("Y/m/d", strtotime($this->end_date)),
]
);
}
Your complete search() method will look like below
public function search($params)
{
$query = Report::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'defaultOrder' => [
'created' => SORT_DESC,
]
],
'pagination' => [
'pageSize' => 100,
],
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'created' => $this->created,
'updated' => $this->updated,
'closed' => $this->closed,
'user_id' => $this->user_id,
'status_id' => $this->status_id,
'location_id' => $this->location_id,
'typeperson_id' => $this->typeperson_id,
'field_cpfcnpj' => $this->field_cpfcnpj,
]);
if ($this->start_date !== null && $this->end_date !== null) {
$query->andFilterWhere(
[
'BETWEEN',
new Expression(
'DATE_FORMAT(created_at,"%Y/%m/%d")'
),
date("Y/m/d", strtotime($this->start_date)),
date("Y/m/d", strtotime($this->end_date)),
]
);
}
$query->andFilterWhere(['between', 'closed', $this->start_closed, $this->end_closed]);
return $dataProvider;
}

Related

Yii2: Add data picker for gridview

I have output code for certain data from related tables. but I also have the task to add 2 search fields by date.
There should be 2 fields created_at and end_at choosing the date in which I would be able to filter out the extra data
my Controller:
{
$dateNowMinusMonth = new \DateTime('-1 month');
$payed = OrderPayment::find()->where(['status' => OrderPayment::STATUS_PAYED])
->andWhere(['>=', 'created_at', $dateNowMinusMonth->format('Y-m-d H:i:s')])
->all();
$orderIds = ArrayHelper::getColumn($payed, 'order_id');
$elements = [];
if ($orders = Order::find()->where(['id' => $orderIds])->all()) {
foreach ($orders as $order) {
foreach ($order->elements as $element) {
$product = $element->product;
if (array_key_exists($product->id, $elements)) {
$elements[$product->id]['count'] += 1;
} else {
$elements[$product->id] = [
'name' => $product->name,
'barcode' => $product->barcode,
'count' => 0,
'amount' => $product->storitem->amount,
'item_cost' => $product->purchase->item_cost,
'price' => $product->price,
];
}
}
}
}
$dataProvider = new ArrayDataProvider([
'allModels' => $elements,
'sort' => [
'defaultOrder' => ['count' => SORT_DESC],
'attributes' => ['name', 'count', 'barcode', 'amount', 'item_cost', 'price']
],
'pagination' => [
'pageSize' => 15,
],
]);
return $this->render('Reporat', [
'dataProvider' => $dataProvider,
]);
}
and view:
<?= GridView::widget([
'id' => 'search-table',
'dataProvider' => $dataProvider,
'striped' => false,
'options' => ['class' => 'text-center'],
'columns' => [
'name',
'barcode',
'item_cost',
'price',
'count',
'amount'
],
]); ?>
Please help me add 2 fields by which I would be able to filter the displayed data by date of creation and end date.

Yii2 Select2 multiple with ajax: Illegal offset type

I use Yii2 and widget Select2. I want onkeyup to search products from table "Products" with options for multiple select, because i must save result in second table "rel_products". I do know why it return error : "Illegal offset type"
Here is model:
public $products = array(); =>because i write result in second table
here is view:
$url = \yii\helpers\Url::to(['/product/prodlist']);
echo $form->field($model, 'products')->widget(Select2::classname(), [
'initValueText' => 'Search for a city ...', // set the initial display text
'model' => $model,
'attribute' => 'products',
'theme' => 'bootstrap',
'options' => ['placeholder' => 'Search for a city ...'],
'pluginOptions' => [
'allowClear' => true,
'minimumInputLength' => 3,
'ajax' => [
'url' => $url,
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
],
'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
'templateResult' => new JsExpression('function(product) { return product.text; }'),
'templateSelection' => new JsExpression('function (product) { return product.text; }'),
],
]);
Here is Controller:
public function actionProdlist($q = null, $id = null) {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$out = ['results' => ['id' => '', 'text' => '']];
if (!is_null($q)) {
$query = new Query;
$query->select()
->from('product')
->joinWith('translation')
->where(['like', 'title', $q])
->limit(20);
$command = $query->createCommand();
$data = $command->queryAll();
$out['results'] = array_values($data);
}
elseif ($id > 0) {
$out['results'] = ['id' => $id, 'text' => Product::find($id)->title];
}
return $out;
}
change in model:
public $products; =>because i write result in second table

What's the difference between ActiveDataProvider and SQLDataProvider?

I'm a little bit confused. I'm wondering what is the difference between Yii2 ActiveDataProvider and SQLDataProvider? I was looking in the documentation for it, but couldn't get it too.
Could someone explain me when should I use one or another? And what is the difference between it?
Thank you for your time.
SqlDataProvider works with a raw SQL statement which is used to fetch the needed data and ActiveDataProvider can take either a yii\db\Query or yii\db\ActiveQuery object.
SqlDataProvider example:
$provider = new SqlDataProvider([
'sql' => 'SELECT * FROM post WHERE status=:status', //HERE
'params' => [':status' => 1],
'totalCount' => $count,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'attributes' => [
'title',
'view_count',
'created_at',
],
],
]);
ActiveDataProvider example:
$query = Post::find()->where(['status' => 1]); // ActiveQuery here
$provider = new ActiveDataProvider([
'query' => $query, // and here
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'defaultOrder' => [
'created_at' => SORT_DESC,
'title' => SORT_ASC,
]
],
]);

Yii2 apply parameters to DB sort

I have the following search function in my BlogPostSearch ActiveRecord:
public function search($params)
{
$query = BlogPost::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'attributes' => [
'publishDate' => [
'asc' => ['(CASE state
WHEN '.static::STATE_ONLINE.' THEN published_at
WHEN '.static::STATE_OFFLINE.' THEN updated_at
END)' => SORT_ASC],
'desc' => ['(CASE state
WHEN '.static::STATE_ONLINE.' THEN published_at
WHEN '.static::STATE_OFFLINE.' THEN updated_at
END)' => SORT_DESC],
'default' => SORT_DESC
],
],
],
]);
return $dataProvider;
}
But part of my soul dies every time I concatenate a parameter into SQL. Is it possible to bind variables in here?
Something like:
...
'asc' => [
'expression'=> [
'(CASE state
WHEN :online THEN published_at
WHEN :offline THEN updated_at
END)' => SORT_ASC
],
'params' => [
'online' => static::STATE_ONLINE,
'offline' => static::STATE_OFFLINE,
],
],
...

Yii2 Order by calculated field

I have two tables:
Orders:
- id
- client
- ...
Lines:
- orderID
- startDate
- endDate
- ...
In my orders' Controller I added these two to get dates from each order lines:
public function getStartDate()
{
return OrdersLines::find()
->andWhere(['orderID'=>$this->id])
->min('startDate');
}
public function getEndDate()
{
return OrdersLines::find()
->andWhere(['orderID'=>$this->id])
->max('endDate');
}
In my Index view (using kartik's grid and expandRowColumn) I show the orders grid with 2 calculated columns:
start_Date: gets the sooner start date from lines for each order
end_Date: gets the higher end date from lines for each order
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'export' => false,
'columns' => [
[
'class' => 'kartik\grid\ExpandRowColumn',
'value' => function ($model, $key, $index, $column) {
return GridView::ROW_COLLAPSED;
},
'detail' => function ($model, $key, $index, $column) {
$searchModel = new OrdersLinesSearch();
$searchModel->orderID = $model->id;
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return Yii::$app->controller->renderPartial('_ordersLines', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
},
],
...
[
'attribute' => 'start_Date',
'format' => 'date',
'label' => 'Start Date',
'value' => 'startDate',
],
[
'attribute' => 'end_Date',
'format' => 'date',
'label' => 'End Date',
'value' => 'endDate',
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
How can I set OrdersSearch model to allow sorting with these values (start_Date and end_Date)?
I think that you should create ActiveDataProvider using join in ActiveQuery, using sort parameter passed to action:
$sort = \Yii::$app->request->get('sort');
if($sort == 'startDate') $having = 'MAX(startDate)';
if($sort == 'endDate') $having = 'MAX(endDate)';
$sql = Orders::find()->joinWith(['lines' => function($q) use($having) {
$q->having = $having;
}]);