addInCondition inside addCondition

Hi

I have a couple of OR conditions - each consisting of 2 nested conditions.

One of the nested conditions is straight forward, but the other is actually an ‘addInCondition’.

Here is an example of one of the OR conditions:


$criteria->addCondition('(val1 = :key AND val2 IN :array)', 'OR');

This works


$criteria->params[':key'] = $myValue;

This line produces an ‘Array to string conversion’ error.


$criteria->params[':array'] = $myArray;

The only way I can get it working is to convert the array to a list e.g. “(‘34’, ‘72’, ‘94’)” and then ‘hardcode’ the list into the code:


$criteria->addCondition('(val1 = :key AND val2 IN '.$myArrayList.')', 'OR');

But there should be a better (more secure) way.

Hi, Gerhard.




$crit2 = new CDbCriteria();

$crit2->addCondition('val1 = :key');

$crit2->params[':key'] = $keyVal;

$crit2->addInCondition('val2', $arrayVals);

$criteria->mergeWith($crit2, 'OR');



addInCondition can take an array as the 2nd parameter, which will be properly escaped.

And, CDbCriteria::mergeWith is very convenient when you want to construct a complicated condition.

Hi, Softark.

Many thanx. Will give it a go and report back.