Yii Framework Forum

Search between two dates of the same attribute active record

(Folumikeidowu) #1

I have this model:

    public function rules()
    return [
        [['transaction_date', 'transaction_amount'], 'required', 'message' => '{attribute} is Required!'],
        [['transaction_date'], 'safe'],
        [['date_start_recent', 'date_end_recent'], 'date'],
        [['transaction_amount'], 'number', 'numberPattern' => '/^\s*[+]?[0-9]*[.,]?[0-9]+([eE][-+]?[0-9]+)?\s*$/', 'message' => '"-" is not allowed. Amount should look like: 0.00, 2230.00 and so on'],
        [['receipt_number'], 'string', 'max' => 50]

public function attributeLabels()
    return [
        'fee_pay_tran_id' => Yii::t('fees', 'Receipt No.'),
        'transaction_date' => Yii::t('fees', 'Payment Date'),
        'transaction_amount' => Yii::t('fees', 'Amount'),
        'receipt_number' => Yii::t('fees', 'Receipt No.'),          

and searchModel

    public function search($params)
    $query = FeePaymentTransaction::find();

    $dataProvider = new ActiveDataProvider([
        'query' => $query,


    if (!$this->validate()) {
        // uncomment the following line if you do not want to any records when validation fails
        // $query->where('0=1');
        return $dataProvider;

        'fee_pay_tran_id' => $this->fee_pay_tran_id,
        'transaction_date' => $this->transaction_date,
        'transaction_amount' => $this->transaction_amount,

    $query->andFilterWhere(['like', 'receipt_number', $this->receipt_number]);

    return $dataProvider;


As shown above, I want to search by transaction_amount and between start_date and end_date, then display result in gridview. My transaction_date is the start_date and end_date… Also, start_date must note be greater than end_date.


    public function actionRecentFeeTransactionReport()
    $searchModel = new FeePaymentTransactionSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('recent-fee-transaction-report', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,

Please note that the search is between transaction_date, which is splitted into start_date and end_date

How do I achieve this.


(Softark) #2

Something like this should work.

$query->andFilterWhere(['>=', 'transaction_date', $this->date_start_recent]);
$query->andFilterWhere(['<=', 'transaction_date', $this->date_end_recent]);

And you may have to adjust the rules. transaction_date should not be required in the search model.

(Folumikeidowu) #3

Where do I add this

$query->andFilterWhere([’>=’, ‘transaction_date’, $this->date_start_recent]);
$query->andFilterWhere([’<=’, ‘transaction_date’, $this->date_end_recent]);

precisely in the model.

(Softark) #4

Add these lines in the search method of the search model.

(Folumikeidowu) #5

Thanks. It works perfectly.