Yii Framework-向Controller注册自定义的Action

来源:互联网 发布:matlab软件下载lvsexz 编辑:程序博客网 时间:2024/05/16 00:57

  由于需要一个级联的DropDown List,而且数据必须从后台获取,所以我的Widget API就没用了,额。

  使用Yii自带的ActiveForm的dropDownList控件,加上Ajax可以实现相应的功能的,但是有几点注意的,记录一下;

  级联列表的代码如下:

<?php $form=$this->beginWidget('CActiveForm', array(    'method'=>'get',    'id'=>"customer-contact-search",    'enableAjaxValidation'=>false,)); ?><?php echo $form->dropDownList(    $contactModel,    'customer_id',    CHtml::listData(CustomerContact::model()->findAll(),'customer_id','customer_id'),    array(        'id'=>'customer_id',        'empty'=>'请选择'.CustomerContact::model()->getAttributeLabel('customer_id'),        'ajax'=>array(            'type'=>'GET',            'data'=>'js:{"customer_id":$("#customer_id").val()}',            'url'=>$this->createUrl('customerContact/getCustomerNameSelectionsByCustomerId'),            'update'=>'#customer_name'        )    )); ?><?php echo $form->dropDownList(    $contactModel,    'customer_name',    CHtml::listData(CustInfo::model()->findAllByAttributes(array('custno'=>$contactModel->customer_id)),        'custno','name'),    array(        'id'=>"customer_name",    )); ?><?php echo CHtml::submitButton('查询',array('class'=>'btn')); ?><?php $this->endWidget(); ?>

   功能非常简单:选择ID,显示相应的名字。

   分析如下:

    选择了ID后,创建一个Router:"customerContact/getCustomerNameSelectionsByCustomerId", 并且使用data属性将选择的值GET过去。

  这里的actionGetCustomerNameSelectionsByCustomerId就是一个自定义的Action。

  最开始我是在Controller里面创建了Action后直接去View查看,dump结果显示“没有相关的权限”。

  因为Controller对于Action的控制使用accessRules方法来控制各个Action的权限的。那就在accessRules中添加我自定义的Action,并附加相应权限就OK了。

   这是原始的accessRules:

   

public function accessRules(){return array(array('allow',  // allow all users to perform 'index' and 'view' actions'actions'=>array('index','view'),'users'=>array('*'),),array('allow', // allow authenticated user to perform 'create' and 'update' actions'actions'=>array('create','update'),'users'=>array('@'),),array('allow', // allow admin user to perform 'admin' and 'delete' actions'actions'=>array('admin','delete'),'users'=>array('admin'),),array('deny',  // deny all users'users'=>array('*'),),);}

这是添加后的accessRules:

   

public function accessRules(){return array(array('allow',  // allow all users to perform 'index' and 'view' actions'actions'=>array('index','view'),'users'=>array('*'),),            array('allow', // allow authenticated user to perform 'create' and 'update' actions                'actions'=>array('create','update','GetCustomerNameSelectionsByCustomerId'),                'users'=>array('@'),            ),array('allow', // allow authenticated user to perform 'create' and 'update' actions'actions'=>array('create','update'),'users'=>array('@'),),array('allow', // allow admin user to perform 'admin' and 'delete' actions'actions'=>array('admin','delete'),'users'=>array('admin'),),array('deny',  // deny all users'users'=>array('*'),),);}

  这里需要注意的地方就是:自定义的Action的权限必须在"deny"的前面,否则不会生效。


  2. 还有一个问题就是在使用CHtml::listData方法时的问题。

   下面是对listData方法的原型叙述:

   

public static array listData(array $models, string $valueField, string $textField, string $groupField='') $models array 模型对象的列表。这个参数也可以是一个关联的数组(例如CDbCommand::queryAll的结果)。 $valueField string 列表选项值的属性名 $textField string 列表选项文本的属性名 $groupField string 列表选项组的属性名。如果是空,将不会生成组。 {return} array 可用于dropDownList, listBox等的列表数据。 
    所以对于传入的model需要使用findAllByAttributes方法,不能使用findByAttributes方法,因为只有前者才会返回一个model的列表。

0 0
原创粉丝点击