Yii框架常见问题汇总
然用过Yii做了一个小项目了,但是过程中间解决的问题没有随手记下来,导致新项目开始后,以前碰到的问题还得在查一遍,干脆就记下来,以便不时之需。
有新的会随时更新。
1.如何显示ActiveRecord执行的sql语句:
array('class'=>'CFileLogRoute','levels'=>'trace,error, warning', ), // uncomment the following to show log messages on web pages /* array('class'=>'CWebLogRoute', ), */
在项目的config/main.php中,找到上面的代码段,添加trace,取消底下一段的注释
2.在生成的_search.php中,如何去掉必须输入项的 "*" 号:
只需要加上一句代码:<?php CHtml::$afterRequiredLabel = '';?>
3.如何处理Model关联的对象为空的情况。
例如:显示员工所属部门,使用TbDetailView时,
'attributes'=>array(......array('label'=>'所属部门','value'=>!empty($model->department)?CHtml::encode($model->department->name) : '未设置'),
4.如何在下拉列表中显示“未选择”。
<?php echo $form->dropDownListRow($model,'type',CHtml::listData(CodeType::model()->findAll(),'id','name'),array('prompt'=>'[未选择]')) ?>
5.如何在TbGridView中显示CStarRating控件:
<?php $this->widget('bootstrap.widgets.TbGridView',array( 'id'=>'program-grid', 'dataProvider'=>$model->search(), 'afterAjaxUpdate'=>'function(id,data){ $("[id^=\'rating\'] > input").rating({"required":true}); $("[id^=\'rating\'] > div").attr("disabled","disabled"); }', //'filter'=>$model, 'type'=>'striped bordered', 'columns'=>array('id','business_id','business_name','program_code.name::程序类型','program_code1.name::开发语言',array('name'=>'level','type'=>'raw', 'value'=>'$this->grid->controller->widget("CStarRating",array("starCount"=>"5","minRating"=>"1","maxRating"=>"10","allowEmpty"=>false, "name"=>$data->id,"id"=>"rating_" .$data->id,"value"=>$data->level,),true)',), array( 'class'=>'bootstrap.widgets.TbButtonColumn', ), ), )); ?>
6.怎样在Grid中对外键关联的字段进行排序:
例如:Model名为Product,在Model里:
public function relations() {// NOTE: you may need to adjust the relation name and the related// class name for the relations automatically generated below.return array('product_agent'=>array(self::BELONGS_TO,'Agent','manufacturer'),); }
public function search() {$criteria=new CDbCriteria;$criteria->with = array('product_agent');...............return new CActiveDataProvider(get_class($this), array('criteria'=>$criteria,'sort'=>array('attributes'=> array('id','register_date','product_name','manufacturer','product_agent.name','unit_price','library_count' )),)); }
在页面里:
<?php $this->widget('zii.widgets.grid.CGridView', array('id'=>'product-grid','dataProvider'=>$model->search(),//'filter'=>$model,'columns'=>array('id', 'product_agent.name::生产厂商',array('class'=>'CButtonColumn','afterDelete'=>'function(link,success,data){if(data != "") alert(data);};'),), )); ?>
7.如何自定义验证:
比如,在出库时判断是否库存不足:
在model中:
public function rules() {// NOTE: you should only define rules for those attributes that// will receive user inputs.return array(....... array('quantity', 'quantityValidator'),); }
/*在库数的验证*/public function quantityValidator($attribute,$params){ if ( $this->quantity > $this->shipment_product->library_count ) {$this->addError('quantity', '库存不足!');}}
8.如何对一个Model中的日期字段按照一个指定范围进行查询:
在model里添加两个字段:
public $occurrence_date_start; public $occurrence_date_end;
然后再search方法中:
public function search() {$criteria=new CDbCriteria;$criteria->compare('id',$this->id);if((isset($this->occurrence_date_start) && trim($this->occurrence_date_start) != "")&& (isset($this->occurrence_date_end) && trim($this->occurrence_date_end) != ""))$criteria->addBetweenCondition('occurrence_date', ''.$this->occurrence_date_start.'', ''.$this->occurrence_date_end.'');......return new CActiveDataProvider(get_class($this), array('criteria'=>$criteria,'sort'=>array('attributes'=>array('id','occurrence_date','shipment_customer.name','shipment_product.product_name','shipment_staff.name','shipment_code.code_name','quantity','total_amount','actual_back_section_date',)),)); }
在前台页面,我这里用的是Yii内置的CJuiDatePicker:
<div class="row"><?php echo $form->label($model,'occurrence_date_start'); ?><?php $this->widget('zii.widgets.jui.CJuiDatePicker', array('model'=>$model,'attribute'=>'occurrence_date_start',// additional javascript options for the date picker plugin'options'=>array('showAnim'=>'fold','showMonthAfterYear'=>'false',),'htmlOptions'=>array('style'=>'height:20px;',),'language'=>'zh_cn',));?></div><div class="row"><?php echo $form->label($model,'occurrence_date_end'); ?><?php $this->widget('zii.widgets.jui.CJuiDatePicker', array('model'=>$model,'attribute'=>'occurrence_date_end',// additional javascript options for the date picker plugin'options'=>array('showAnim'=>'fold','showMonthAfterYear'=>'false',),'htmlOptions'=>array('style'=>'height:20px;',),'language'=>'zh_cn',));
From: http://www.cnblogs.com/dahuzizyd/archive/2013/03/26/2983471.html
转载于:https://www.cnblogs.com/imxiu/p/3451759.html
Yii框架常见问题汇总相关推荐
- php事件和行为,Yii框架组件和事件行为管理详解
Yii框架组件和事件行为管理详解 来源:中文源码网 浏览: 次 日期:2018年9月2日 [下载文档: Yii框架组件和事件行为管理详解.txt ] (友情提示:右键点上行txt文档名- ...
- 软件测试——接口常见问题汇总
目录 前言 一.接口用例设计 二.接口常见问题汇总 结语 前言 今天我们来聊聊接口设计用例设计,说到这个接口,相信绝大多数的测试员都有遇到过某些棘手的问题,那么今天我们就来总结一下在接口方面会遇到的难 ...
- 神经网络学习小记录-番外篇——常见问题汇总
神经网络学习小记录-番外篇--常见问题汇总 前言 问题汇总 1.下载问题 a.代码下载 b. 权值下载 c. 数据集下载 2.环境配置问题 a.20系列所用的环境 b.30系列显卡环境配置 c.CPU ...
- HoloLens 2 打包发布过程中 常见问题汇总(长期更新)
HoloLens 2 打包发布过程中 常见问题汇总(长期更新) 目 录 1. Unity 中发布Hololens的时候界面变灰,提示无发布权限. 2. Unity发 ...
- PHP开发框架之YII框架学习——碾压ThinkPHP不是梦
前 言 JRedu 程序猿是一种慵懒的生物!能少敲一行代码,绝对不会多敲一个字符!所以,越来越多的开发框架应运而生,在帮助我们完成功能的同时,极大程度上也帮我们节省了人力物力,而且也提高了系统的 ...
- exec不同文件l怎么汇总_ABAQUS常见问题汇总 - 2.0版.doc
您所在位置:网站首页 > 海量文档  > 计算机 > C/C++资料 ABAQUS常见问题汇总 - 2.0版.doc1 ...
- php框架里有模版引擎吗,Yii框架用模版引擎了吗?_PHP开发框架教程
Yii框架用模版引擎了吗? Yii框架没有用模版引擎,Yii照样采纳原始PHP作为模板语法,该框架是一个基于组件的高性能PHP框架,用于开发大型Web运用,而且采纳严厉的OOP编写,并供了Web运用开 ...
- 跨平台工具、组件和框架的汇总
本文主要是对跨平台工具.组件和框架的汇总,包括游戏引擎.编程语言.移动开发.网络通信.图形界面等等 Qt是1991年奇趣科技开发的一个跨平台的C++图形用户界面应用程序框架.它提供给应用程序开发者建立 ...
- 使用maven下载依赖包及maven常见问题汇总
最近下载了SPRING3.1.4,发现只有SPRING相关的源码,没有其依赖的jar包.SPRING依赖的jar相当多,自己一个一个的下载比较费劲,就仔细阅读了SPRING下载说明,新版本的SPRIN ...
最新文章
- Visio对象插入Word后周围空白过大
- Redis 常见命令
- python学习笔记一
- js 正则 显示千分号 支持整数和小数
- 主存和cache每一块相等_CPU中的Cache实现原理
- 计算机网络(二)——局域网硬件设备
- 玩转 SpringBoot 2 快速整合 Filter 注解版
- Python基础篇:Python 程序流程控制,让你的代码有理有据
- 数据库 -- MySQL使用
- htons htonl ntohl ntohs 的区别和作用
- redis 备份与恢复
- 德国人制作的 Windows 2000/XP 优化脚本2.2_build9 (2008-05-31)
- cocos2d-x 使用BMFont生成中文字体并应用
- 海外博士申请经历分享
- Kotlin-协程Coroutines-组合suspending暂停函数
- 如何做一名优秀的程序员?
- 并发编程中你需要知道的基础概念
- 单目标跟踪SiamMask:特定目标车辆追踪 part2
- Java后端工程师学习路线——大学版
- Android ViewGroup介绍+实例,大厂架构师经验分享