I have gone through a number of tutorials and have modified this code several times but can’t figure out why it is not working. Pls someone should help me out.
I have a method in my controller class (/protected/controller/BrandController.php)which is:
public function actionBrandSearch($term)
{
//$term = $_GET['term'];
$query ="SELECT name FROM fbv_brand WHERE name LIKE :string";
$command =Yii::app()->db->createCommand($query);
$command->bindValue(":string", '%'.$term.'%', PDO::PARAM_STR);
$result =$command->queryAll();
echo CJSON::encode($result);
Yii::app()->end();
}
Then I have in (/protectedd/view/brand/_form.php)
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl('brand'),
'method'=>'get',
)); ?>
<div class="row">
<?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'name',
'sourceUrl'=>$this->createUrl('brand/brandSearch'),// <- path to controller which returns dynamic data
// additional javascript options for the autocomplete plugin
'options'=>array(
'showAnim'=>'fold',
'minLength'=>'1', // min chars to start search
'select'=>'js:function(event, ui) { alert(ui.item.id +":"+ui.item.value); }'
),
));
?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
Please what is missing, what am I failing to do? I access this form with r=brand/create, the form shows but no response from db.
I have implemented the above suggestion but did NOT get any results.
My questions:
(i) Is it compulsory to attach a model to the CJuiautocomplete, as someone said in one tutorial. I have done that but no results obtained. If it is compulsory, maybe I did not do it well.
It is not compulsory, but using ‘model’ and ‘attribute’ pair instead of ‘name’ and ‘value’ pair will make things easier when you want to use the widget in CActiveForm.