Yii Framework Forum: Addincondition And Condition With Params - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Addincondition And Condition With Params Rate Topic: -----

#1 User is offline   Liam Carter 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 30-August 12

Posted 06 February 2013 - 07:59 AM

Hiya,

It took a bit of time for me to work this out so it might help someone.

I was having a great trouble with CDbCriteria having a condition, a addInCondition and a params for the condition field.

I was using this code

		$criteria = new CDbCriteria;
		$criteria->condition = 'period=:period';
		$criteria->addInCondition('number',$numbers);
		$criteria->params = array(':period'=>$period);


I was getting this error

CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens. The SQL statement executed was: SELECT COUNT(*) FROM `data` `t` WHERE (period=:period) AND (number IN (:ycp0, :ycp1, :ycp2, :ycp3, :ycp4, :ycp5))


I found the only solution was to change it so the params were specified straight after the condition and before the addInCondition like this:

$criteria = new CDbCriteria;
		$criteria->condition = 'period=:period';
		$criteria->params = array(':period'=>$period);
		$criteria->addInCondition('number',$numbers);


This then worked.

Hope this helps someone.
0

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 06 February 2013 - 09:34 AM

Dear Friend

Yes you are absolutely correct.

This happens because method CDbCriteria::addInCondition internally creates the params for each element in the array.

If we declare the params at the end of code, we are completely resetting the params overiding parameters set
in the method CDbCriteria::addInCondition.

To circumvent this we have to do the following.
$criteria = new CDbCriteria;
$criteria->condition = 'period=:period';
$criteria->addInCondition('number',$numbers);
$criteria->params = array(':period'=>$period)+$criteria->params;//+ array union operator


Regards.
2

#3 User is offline   adreno 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 31-May 13

Posted 22 September 2013 - 04:06 AM

Excellent fix regarding the overwriting of parameters, just spent 2.5 hours trying to work out what was going on. Your post just made it all work. Thanks! :D
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users