3

我只是yii 框架的新手。目前我有两张桌子,一张sales,另一张stores Sales table看起来像这样

==============
      sales
  ==============
  id
  store_id

store table看起来像这样

 ==============
      Stores
  ==============
  id
  store_name
  store_location

现在在销售视图表单(_form.php)中,我已经渲染了销售和商店。在销售控制器中,动作创建的代码是这样的

  public function actionCreate()
  {
    $model=new Sales;
    $stores = new Stores;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Sales']$_POST['Stores']))
    {
      $model->attributes=$_POST['Sales'];
      $stores->attributes = $_POST['Stores'];
      $valid = $model->validate();
      $valid = $stores->validate();
      if($valid)
      {
        $stores->save(false);
        $model->store_id = $stores->getPrimaryKey();
        $model->save(false);
        $this->redirect(array('view','id'=>$model->id));
      }
    }

    $this->render('create',array(
      'model'=>$model,
      'stores' => $stores,
    ));
  }

为了在下拉列表中获取所有商店名称,我编写了这样的代码

    <div class="row">
    <?php echo $form->labelEx($stores,'store_name'); ?>
    <?php echo $form->dropDownList($stores,'store_name', CHtml::listData(Stores::model()->findAll(), 'store_name', 'store_name'), array('empty'=>'--Select--')) ?>
    <?php echo $form->error($stores,'store_name'); ?>
  </div>

但在这里我想要 cjuiautocomplete 字段,这样当有人按下任何键时,它就会开始显示建议商店名称。为此,我刚刚通过此链接

就像文档一样,我在受保护/扩展目录下制作了 EAutoCompleteAction.php 然后我在销售控制器中制作了这样的控制器代码

 public function actions()
    {
      return array(
        'aclist'=>array(
          'class'=>'application.extensions.EAutoCompleteAction',
          'model'=>'Stores', //My model's class name
          'attribute'=>'store_name', //The attribute of the model i will search
        ),
      );
    }

并在销售(_form.php)的视图文件中,我制作了这样的代码

    <div class="row">
    <?php echo $form->labelEx($stores,'store_name'); ?>
    <?php 
  $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
      'attribute'=>'store_name',
        'model'=>$stores,
        'sourceUrl'=>array('stores/store_name'),
        'name'=>'store_name',
        'options'=>array(
          'minLength'=>'3',
        ),
        'htmlOptions'=>array(
          'size'=>45,
          'maxlength'=>45,
        ),
  )); ?>

毕竟,当我通过关键字进行搜索时,它在 firebug 的控制台面板中显示 404 错误。萤火虫中请求的搜索网址是这样的(广告是我在商店名称字段中的搜索查询)

http://localhost/WebApp/index.php?r=stores/store_name&term=ads

有人在这里寻求帮助吗?

4

1 回答 1

0

您忘记将操作名称从示例更改acliststore_name

public function actions()
{
  return array(
    'store_name'=>array( // << Array key is action name
      'class'=>'application.extensions.EAutoCompleteAction',
      'model'=>'Stores', //My model's class name
      'attribute'=>'store_name', //The attribute of the model i will search
    ),
  );
}
于 2013-07-15T14:01:43.107 回答