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
- Yii Framework-向Controller注册自定义的Action
- Yii获取controller和action的方法
- YII controller action
- yii中Controller中的Action直接跳到另一个action的方法(yii学习)
- Yii里获取当前controller和action的id
- 如何在yii的controller中调用外部action
- Yii里获取当前module controller action的id
- Yii中获取当前controller和action的id
- 如何在yii的controller中调用外部action
- Yii Framework Model-View-Controller (MVC)
- YII Framework学习教程-YII的Model-Action和Models-2011-11-23
- Yii在Controller向layout传值
- yii 在controller里面控制全局所有action的侧边栏或者导航栏的菜单
- Play framework Action、Controller和Result
- YII Framework学习教程-YII的Model-数据库操作3-自定义的DAO操作
- YII Framework学习教程-YII的Model-数据库操作3-自定义的DAO操作
- Asp.net Mvc Framework可以在Controller中使用的Url.Action方法
- Asp.net Mvc Framework可以在Controller中使用的Url.Action方法
- HDU1231 最大连续子序列
- 用Intent发短信和彩信
- IOS单例模式及单例模式的优缺点
- note : 检测并激活已经运行的实例
- XTOJ 1168 Alice and Bob (记忆化搜索)
- Yii Framework-向Controller注册自定义的Action
- TCP协议中的三次握手和四次挥手(图解)
- Win7+Editplus配置Python的开发环境
- POJ3624 Charm Bracelet
- Linux--根文件系统的挂载过程分析
- 计算机是如何启动的(以Windows和linux为例)
- for和Iterator遍历速度比较
- CC2530学习笔记のGPIO配置——点亮LED
- Mac语言那些事,切换系统语言和程序语言