日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Yii框架常见问题汇总

發布時間:2023/12/31 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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框架常见问题汇总的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。