hello all,
i have problem with search date range Yii2 with unix timestamp. i cannot search created_at of my data i’ve try to follow some tutorial but still not work.
here’s my modelSearch
public function search2($params)
{
$query = AskPolicy::find();
$query-> innerJoinWith(['askPos'])
->andwhere("ask_policy.id = ask_po.id_policy")
->innerjoinWith(['client'])->andWhere(['id_client'=>Yii::$app->user->identity->id_client])->all();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'ask_policy.created_at' => $this->start_date,
'ask_policy.created_at' => $this->end_date,
'printed_at' => $this->printed_at,
]);
// $query->andFilterWhere(['>=', 'ask_policy.created_at',$this->start_date]);
// $query->andFilterWhere(['<=', 'ask_policy.created_at',$this->end_date]);
$query->andFilterWhere(['>=',"(date_format(FROM_UNIXTIME(ask_policy.`created_at` ), '%Y-%m-%d' ))", $this->start_date])
->andFilterWhere(['<=', "(date_format(FROM_UNIXTIME(ask_policy.`updated_at` ), '%Y-%m-%d' ))", $this->end_date]);
return $dataProvider;
}
here’s my controller
public function actionIndex()
{
$searchModel = new AskPolicySearch();
$dataProvider = $searchModel->search2(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
here’s my form _search and index page(grid)
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
// use yii\jui\DatePicker;
use trntv\yii\datetime\DateTimeWidget;
/* @var $this yii\web\View */
/* @var $model backend\modules\ask_marine_cargo\models\search\AskPolicySearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="form-ask-policy-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'id', ['template' => '{input}'])->textInput(['style' => 'display:none']); ?>
<?php echo $form->field($model, 'start_date')->widget(
DateTimeWidget::className(),
[
'phpDatetimeFormat' => 'yyyy-MM-dd'
]
) ?>
<?= $form->field ( $model, 'end_date' )->widget (DateTimeWidget::classname (),
[
'phpDatetimeFormat' => 'yyyy-MM-dd'
])
?>
<?php /* echo $form->field($model, 'id_client')->widget(\kartik\widgets\Select2::classname(), [
'data' => \yii\helpers\ArrayHelper::map(\backend\modules\ask_marine_cargo\models\AskClient::find()->orderBy('id')->asArray()->all(), 'id', 'id'),
'options' => ['placeholder' => Yii::t('ask_marine_cargo', 'Choose Ask client')],
'pluginOptions' => [
'allowClear' => true
],
]); */ ?>
<?php /* echo $form->field($model, 'delete_on')->textInput(['placeholder' => 'Delete On']) */ ?>
<?php /* echo $form->field($model, 'printed_at')->textInput(['placeholder' => 'Printed At']) */ ?>
<div class="form-group">
<?= Html::submitButton(Yii::t('ask_marine_cargo', 'Search'), ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton(Yii::t('ask_marine_cargo', 'Reset'), ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
maybe somebody could give step by step for solve my problem. thank you so much