search with Grid View

So I have the following code:




        $q = $_GET['q'];

        $criteria = new CDbCriteria;

        $criteria->select = 't.name, t.description';

        $criteria->addSearchCondition('t.description', ':a', true, 'OR');

        $criteria->addSearchCondition('t.name', ':b', true, 'OR');

        $criteria->params = array(':a' => $q, ':b' => $q);

        $criteria->with = 'comments';

        

        $dataProvider = new CActiveDataProvider("Post", array('criteria' => $criteria));

     

        $this->render('list', array('dataProvider' => $dataProvider));

that should allow a user to search the name and description of Posts in the database, but it keeps giving me the following error:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined. The SQL statement executed was:

what should I do?

I would try




        $q = $_GET['q'];

        $criteria = new CDbCriteria;

        $criteria->select = 't.name, t.description';

        $criteria->addSearchCondition('t.description', $q, true, 'OR');

        $criteria->addSearchCondition('t.name', $q, true, 'OR');

        $criteria->with = 'comments';

        

        $dataProvider = new CActiveDataProvider("Post", array('criteria' => $criteria));

     

        $this->render('list', array('dataProvider' => $dataProvider));



I think you’ve got too much indirection on the parameters. addSearchCondition already parametrizes those values. This should work:




        $q = $_GET['q'];

        $criteria = new CDbCriteria;

        $criteria->select = 't.name, t.description';

        $criteria->addSearchCondition('t.description', $q, true, 'OR');

        $criteria->addSearchCondition('t.name', $q, true, 'OR');

        $criteria->with = 'comments';

        

        $dataProvider = new CActiveDataProvider("Post", array('criteria' => $criteria));

     

        $this->render('list', array('dataProvider' => $dataProvider));