yii2 between query

Hi, Is below code is correct?

controller//

public function actionSearch()

{


	$salesReport= new SalesReport();


	if ($salesReport->load(Yii::$app->request->post()))


	//$salesReport->load(Yii::$app->request->post());


	//echo $salesReport->fromDate, $salesReport->toDate;


{


	$query = SalesOrder::find()->where('between','created_date', $salesReport->fromDate, $salesReport->toDate)->all();


	


    $dataProvider = new ActiveDataProvider([


        'query' => $query,


    ]);








	


	return $this->render('sales-order/index', [            


        'dataProvider' => $dataProvider,


    ]);


} 


}

the debugger shows the passed value. when i tried to get the set value like this

$salesReport->toDate

it returns null.

and if pass the value like this also it show exception as invalid argument passed

where([‘between’, ‘created_date’, ‘2014-11-08’, ‘2014-11-19’])

Remove ->all(). You have to pass a query to ActiveDataProvider, not the results of the query.

And ->where() can use a single string or an array, not several strings. So the second approach of you is right.

And you can surround code with with the code tags in your next post so that it is more readable.


$qString1=':fromDate';

		$qString2=':toDate';

    	//$query = AbiSalesOrder::find()->where(['between','created_date', ':fromDate', ':toDate']);	

//$query->addParams([':fromDate' => $salesReport->fromDate,':toDate' => $salesReport->toDate]);

$query = AbiSalesOrder::find()->where(['between','created_date',$qString1,$qString2]);	

$query->addParams([':fromDate' => $salesReport->fromDate],[':toDate' => $salesReport->toDate]);

		

        $dataProvider = new ActiveDataProvider([

            'query' => $query,

        ]);


	

    	

    	return $this->render('index',[            

            'dataProvider' => $dataProvider,

        ]);

it throws this exception … please guide me. i have been stuck with this for 2 days… Is it my mistake or some kind of bug??

Database Exception – yii\db\Exception

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

The SQL being executed was: SELECT COUNT(*) FROM abi_sales_order WHERE created_date BETWEEN ‘:fromDate’ AND ‘:toDate’

Error Info: Array

(

[0] => HY093


[1] => 0

)

Caused by: PDOException

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

It helped me a little … but some exception has been thrown… Thanks

Was it your intention to introduce parameters now? Anyway, you should use addParams like this:




$query->addParams([':fromDate' => $salesReport->fromDate, ':toDate' => $salesReport->toDate]);

One array, not two. It’s not surprising that it complains about the number of bound variables. But I’m not sure with the parameters in the where clause method. Never done this before, but may work as expected.

I did not use add params… Instead I used like this.


find() -> where(['between', 'created_date', $salesReport -> fromDate, $salesReport -> toDate]);

It works fine now. Thanks for your inputs…