How to Yii SQL Like

I wish to do a search function for my yii project. I found out in yii that is a function called addSearchCondition(), then I tried to do the search function.

public function actionSearchApp(){





$searchapp = $_POST['searchapp'];








$id = Yii::app()->user->getState('id');


$models = GamesDevelopersApp::model()->findAll('developer_id='.$id);


array('models'=>$models);


foreach($models as $model){ 


    $gametitle = CHtml::encode($model->gametitle);





}





$search = new CDbCriteria();


$search->addSearchCondition($gametitle.'LIKE:'.$searchapp);


$result = GamesDevelopersApp::model()->findAll($search);


print_r($result);

}

Error Message: Missing argument 2 for CDbCriteria::addSearchCondition()

Any Suggestion for doing SQL Like in Yii ? or any good example for doing search function in yii

instead of:

$search->addSearchCondition($gametitle.‘LIKE:’.$searchapp);

do:

$search->addSearchCondition($gametitle, $searchapp);

first param is db column, second is condition. Third is escape, whether the keyword should be escaped if it contains characters % or _

please check:

http://www.yiiframework.com/doc/api/1.1/CDbCriteria#addSearchCondition-detail

1 Like

Hi thanks for your reply … I tried … still not work …

Error Message : CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘GG’ in ‘where clause’. The SQL statement executed was: SELECT * FROM games_developers_app t WHERE GG LIKE :ycp0

GG is the last row of my data …

$search->addSearchCondition($gametitle, $searchapp);

$gametitle should represent DB column name from table that U are searching from, or from related tables (but that it has to be in format relationName.column_name).

Can U tell me what table are U searching for and from wnat table $gametitle is ?

1 Like

Im using GamesDevelopersApp() model to find the gametitle where the developer_id=‘correct id’, then from all the gametitle LIKE the “I have entered in search field” to find the result I need

this should do you don’t have to include ‘LIKE’ operator


$search->addSearchCondition($gametitle, $searchapp);