<?php

class SuggestAction extends CAction {

    public $modelClass;
    public $attribute = 'title';

    public function run($q, $limit) {
        $attribute = $this->attribute;
        if (($q = trim($q)) !== '') {

            $models = CActiveRecord::model($this->modelClass)->findAll(array(
                'condition' => "t.{$attribute} LIKE :keyword",
                'order' => "t.{$attribute}",
                'limit' => $limit,
                'params' => array(":keyword" => "%{$q}%")
            ));

            $suggest = array();
            if ($models) {
                foreach ($models as $model) {
                    $suggest[] = $model->$attribute . '|' . $model->primaryKey;
                }
            }
            echo implode("\n",$suggest);
        }
    }

}
