Yii addCondition

hai all…I have sitution could any one please help me

i tried to add a condition tomy critiria like this


$criteria->addCondition("(SELECT * FROM offer_events WHERE enddate >= '$now' AND (title like '%$locationdet%' OR description like '%$locationdet%') ORDER BY id DESC) ");

shows me error

please help me…

"Condition" means "WHERE condition" in this context. SELECT, FROM and ORDER BY parts of the generated query cannot be modified via this method.

And please don’t concatenate variables into the SQL string. Even if they don’t contain untrusted input data right now, your code may change in the future. Try this:




$criteria->addCondition('enddate >= :now AND (title like :locationdet1 OR description like :locationdet2)');

$criteria->params[':now'] = $now;

$criteria->params[':locationdet1'] = $criteria->params[':locationdet2'] = '%' . $locationdet . '%';



or:




$criteria->compare('enddate', '>=' . $now);

$searchCriteria = new CDbCriteria();

$searchCriteria->addSearchCondition('title', $locationdet);

$searchCriteria->addSearchCondition('description', $locationdet, true, 'OR');

$criteria->mergeWith($searchCriteria);




$criteria->addCondition("(SELECT * FROM offer_events WHERE enddate >= '$now' AND (title like '%$locationdet%' OR description like '%$locationdet%') ORDER BY id DESC) ");

Inside single quotes(’’) you can’t put php variable. it is treated as string.

use double quotes instead.