search with autocomplete

Please help me!

I have problem with search in autocomplete.


in autocomplete I want to search both name and port_code. But I don’t know how to do it!

this is my controller

public function actionPort() {

        $criteria = new CDbCriteria;

        $criteria->select = array('id', 'name','port_code');

        $criteria->addSearchCondition('name',  strtoupper( $_GET['term']) ) ;

        $criteria->limit = 15;

        $data = Port::model()->findAll($criteria);

        $arr = array();



        foreach ($data as $item) {


            $arr[] = array(

                'id' => $item->id,

                'value' => $item->name." (".$item->port_code.")",

                'label' => $item->name." (".$item->port_code.")",





        echo CJSON::encode($arr);




thank! :)

Do you mean, when you start typing either a name or a code, suggestions should return both ?

If yes, you can add a search condition to your criteria :

$criteria->addSearchCondition('name',  strtoupper( $_GET['term']) ) ;

$criteria->addSearchCondition('port_code', strtoupper ( $_GET['term']), true, 'or');

This will generate a SQL query like

SELECT id, name, port_code FROM Port WHERE name like '%<term>%' OR port_code like '%<term>%'

Thank you so much tof! now I work for me!! :)