karanlv
(Karanlv012)
November 19, 2014, 3:40pm
1
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’])
robsch
(Robert Schneider)
November 20, 2014, 8:02am
2
karanlv:
$query = SalesOrder::find()->where('between','created_date', $salesReport->fromDate, $salesReport->toDate)->all();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
.
.
.
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.
karanlv
(Karanlv012)
November 21, 2014, 3:18pm
3
$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
karanlv
(Karanlv012)
November 21, 2014, 3:20pm
4
robsch:
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.
It helped me a little … but some exception has been thrown… Thanks
robsch
(Robert Schneider)
November 24, 2014, 4:07pm
5
karanlv:
$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,
]);
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.
karanlv
(Karanlv012)
November 25, 2014, 7:27am
6
robsch:
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…