惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
The Last Watchdog
The Last Watchdog
Cyberwarzone
Cyberwarzone
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
L
Lohrmann on Cybersecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
The Cloudflare Blog
V
V2EX
博客园_首页
博客园 - 聂微东
Vercel News
Vercel News
人人都是产品经理
人人都是产品经理
G
GRAHAM CLULEY
T
Tenable Blog
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
SecWiki News
SecWiki News
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
博客园 - 【当耐特】
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
A
About on SuperTechFans
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
D
DataBreaches.Net
P
Privacy & Cybersecurity Law Blog
Schneier on Security
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
D
Docker
P
Proofpoint News Feed

博客园 - Cure

Oracle 查看哪个表被锁定,并获取对应的sessionID Yii框架中安装srbac扩展方法 Yii框架中使用PHPExcel导出Excel文件 Yii框架中使用SRBAC作为权限管理模块时遇到的问题 文件名过长,无法删除的解决办法 Yii框架中使用bootstrap SilverLight中显示上标,下标,平方 10 Websites To Download Free HTML/CSS Templates 经过两天的努力,终于写出了第一个android的helloword sqlplus 帮助无法显示问题的解决 COBOL中USAGE的用法 asp.net mvc开发项目的部署?? 初试asp.net mvc感受 临时搞两天VC,在VC里如何获取当前程序的名字和路径以及如何分割字符串 Ruby on rails开发从头来(五十九)- ActiveRecord基础(预加载子记录) Borland以2300万美元卖掉CodeGear开发工具部门 Ruby on rails开发从头来(五十八)- ActiveRecord基础(自关联) 下载安装了ubuntu 8.04,感觉很好很强大 项目管理工具Redmine + SubVersion + Apache + windows环境安装搭建
Yii框架常见问题汇总
Cure · 2013-03-26 · via 博客园 - Cure

虽然用过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',
    ));

 9.在使用ajaxButton时如何在controller中调用var_dump等来调试:

<?php echo CHtml::ajaxButton('保存', array('reviewd/create'),
array(//'success'=>'js:function(data){alert(123);}',        
'data'=>array('name'=>'...',
'department_id'=>'...'),
'type'=>'POST',
'update'=>'#review-d-form',
//'return'=>true,
)
,array('class'=>'btn btn-success btn-normal')
)?>

注释掉红色的部分。