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
Yii框架常见问题汇总的更多相关文章
- J2EE进阶(十)SSH框架整合常见问题汇总(一)
SSH框架整合常见问题汇总(一) 前言 以下所列问题具有针对性,但是遇到同类型问题时均可按照此思路进行解决. HTTP Status 404 - No result defined for actio ...
- Nuxt.js的踩坑指南(常见问题汇总)
本文会不定期更新在nuxt.js中遇到的问题进行汇总.转发请注明出处,尊重作者,谢谢! 强烈推荐作者文档版踩坑指南,点击跳转踩坑指南 在Nuxt的官方文档中,中文文档和英文文档都存在着不小的差异. 1 ...
- 《PDF.NE数据框架常见问题及解决方案-初》
<PDF.NE数据框架常见问题及解决方案-初> 1.新增数据库后,获取标识列的值: 解决方案: PDF.NET数据框架,已经为我们考略了很多,因为用PDF.NET进行数据的添加操作时 ...
- yii框架安装心得
最近在学习yii框架, 现在将遇到的一些问题和解决方法写出来与大家分享. yii框架的安装: 下载yii框架之后, 打开文件运行init.bat文件, 如果闪退就打开php的扩展(php_openss ...
- Yii框架 400 错误
YII 400错误 在YII框架中400错误是csrf校验失败的意思 csrf是什么? CSRF(Cross-site request forgery跨站请求伪造,也被称为"One Cli ...
- Yii框架CURD方法
在YII框架中,CURD有2种方式: 1.AR模式:2. DAO模式 AR模式下 查全部 MODEL $model->find()->asArray()->all()查单 个 ...
- CentOS安装Oracle数据库详细介绍及常见问题汇总
一.安装前准备 1.软件硬件要求 操作系统:CentOS 6.4(32bit)Oracle数据库版本:Oracle 10g(10201_database_linux32.zip)最小内存:1G(检查命 ...
- SVN集中式版本控制器的安装、使用与常见问题汇总
SVN是Subversion的简称,是一个开放源代码的版本控制系统,它采用了分支管理系统,集中式版本控制器 官方网站:https://www.visualsvn.com/ 下载右边的服务器端,左边的客 ...
- Yaf(Yet Another Framework)用户手册 yii框架手册
地址:http://www.laruence.com/manual/ yaf框架手册:http://pan.baidu.com/s/1bnHFPHd yii框架手册:http://pan.baidu. ...
随机推荐
- 网站开发常用jQuery插件总结(五)滚动条插件nanoscroller
网站在展示信息时,如果信息量过大,解决方法主要有三种.1.分页,将信息分页显示.2.整页显示,但是页面过长,影响浏览体验.3.使用滚动条,而默认滚动条样式太单一,用户体验不友好.所以我们需要美化滚动条 ...
- (jQuery 插件)封装容器的表单为json对象
下面代码可以把一个页面容器中的表单元素封装成一个json对象. (function($){ $.fn.serializeObject=function(){ var inputs=$(this).fi ...
- yii2源码学习笔记(十七)
Theme 类,应用的主题,通过替换路径实现主题的应用,方法为获取根路径和根链接:yii2\base\Theme.php <?php /** * @link http://www.yiifram ...
- 犯这个错误的肯定不止我一个 关于File
File.Create(string filePath)这种用法所有人都知道,这两天用到的时候却发现一个问题. 需要先判断文件是否存在,如果不存在则创建文件,然后向该文件写入数据,后续定时Append ...
- Activity的测量(Measure)、布局(Layout)和绘制(Draw)过程分析
一个Android应用程序窗口里面包含了很多UI元素,这些UI元素是以树形结构来组织的,即它们存在着父子关系,其中,子UI元素位于父UI元素里面,因此,在绘制一个Android应用程序窗口的UI之前, ...
- linux安装ruby ruby-devel rubygems bundler
linux安装ruby ruby-devel rubygems yum install ruby ruby-devel rubygems 安装bundler gem install bundleror ...
- NS实现采用的技术大多是PHP,如果采用java、 .net是否同样适用?
SNS采用的技术可不都是PHP (不局限于国内),特别是国外的新兴公司,基本上没有再用PHP的了,国内到还是蛮常用的.简单说说我知道的几个案例:Facebook (PHP):Facebook采用PHP ...
- Python读写Redis数据库
import redis class Database: def __init__(self): self.host = 'localhost' self.port = 6379 def write( ...
- VCC,VDD,VEE,VSS,VPP 表示的意义
转自VCC,VDD,VEE,VSS,VPP 表示的意义 VCC,VDD,VEE,VSS,VPP 表示的意义 版本一: 简单说来,可以这样理解: 一.解释 VCC:C=circuit 表示电路的意思, ...
- 周鸿祎——不要抱着打工心态去工作,而是把工作当创业(附读书笔记) good
360周鸿祎反而最为开明,他说“不要抱着打工心态去工作,而是把工作当创业”,就像是,鼓励你拿着公司的工资,锻炼自己的能力,为自己以后创业积累资源和人脉(读书笔记:真是天底下最好的机会,天底下没有比工作 ...