Yii筆記:列印sql、Form表單、時間外掛、Mysql的 FIND_IN_SET函式使用、是否是post/ajax請求

OldBoy~發表於2018-11-02
語句部分:
yii1版本列印最後一條執行的SQL: $this->getDbConnection()->createCommand()->select()->from()->order()->text ;
yii2版本列印最後一條執行的SQL: $query = InsuranceOnlineZaSuccess::find(); echo $query->createCommand()->getRawSql();
獲取最後一條入庫的ID:$id  = $this->getDbConnection()->getLastInsertID();     Yii::app()->db->getLastInsertID();
 
view列表處理部分:
yii2在處理gii生成的list(index)列表筆記:
 <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [

            [
                'attribute' => 'id',  //保持可以排序,必須保留attribute屬性,如果去除排序,可以使用header屬性命名
              'value' => 'id',
               'contentOptions' => [ 'width' => '35px' ],
                'attribute' => 'id',
            ],
            'user',
            'policyNo',
            'expiryDate' =>  [
                'attribute' => 'expiryDate',
                'value' =>  function ($model) {  //使用函式來處理屬性值
                  return $model->FormatData($model->expiryDate);
                }]
            ,
            'insurance_online_id',
            // 'create_time',

            ['class' => 'yii\grid\ActionColumn',
                    'template'=>'{delete}'  //這裡可以設定只保留的操作
                ],
        ],
    ]); ?>

Form表單部分:

1、元件使用方法:
文字框:textInput();
密碼框:passwordInput();
單選框:radio(),radioList();
核取方塊:checkbox(),checkboxList();
下拉框:dropDownList();
隱藏域:hiddenInput();
文字域:textarea([‘rows’=>3]);
檔案上傳:fileInput();
提交按鈕:submitButton();
重置按鈕:resetButtun();

<?php
$form = ActiveForm::begin(['action' => ['test/getpost'],'method'=>'post',]); ?>

<? echo $form->field($model, 'username')->textInput(['maxlength' => 20]) ?>

<? echo $form->field($model, 'password')->passwordInput(['maxlength' => 20]) ?>

<? echo $form->field($model, 'sex')->radioList(['1'=>'','0'=>'']) ?>

<? echo $form->field($model, 'edu')->dropDownList(['1'=>'大學','2'=>'高中','3'=>'初中'], ['prompt'=>'請選擇','style'=>'width:120px']) ?>

<? echo $form->field($model, 'file')->fileInput() ?>

<? echo $form->field($model, 'hobby')->checkboxList(['0'=>'籃球','1'=>'足球','2'=>'羽毛球','3'=>'乒乓球']) ?>

<? echo $form->field($model, 'info')->textarea(['rows'=>3]) ?>

<? echo $form->field($model, 'userid')->hiddenInput(['value'=>3]) ?>

 <?= $form->field($model, 'userid')->hiddenInput(['value'=>3])->label(false); ?>

<? echo Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>

<? echo Html::resetButton('重置', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>

<?php ActiveForm::end(); ?>

時間外掛

use yii\jui\DatePicker;
 <?= $form->field($model, 'expiryDate')->textInput(['maxlength' => true])->widget(\yii\jui\DatePicker::className(),[
        'language'=>'zh-CN',
        'dateFormat' => 'yyyy-MM-dd'

    ]) ?>
<?php
            echo "開始時間:";
            Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');
            $this->widget('CJuiDateTimePicker', array(
                'name' => 'SplashConfig[begin_time]',
                'value' => $model->begin_time,
                'mode' => 'datetime', //use "time","date" or "datetime" (default)
                'options' => array(
                    'dateFormat' => 'yy-mm-dd'
                ), // jquery plugin options
                'language' => 'zh',
                'htmlOptions' => array('style' => "width:123px","readonly" => "readonly")
            )); ?>

YII使用Mysql FIND_IN_SET函式

if ($this->city_ids) {
    $criteria->addCondition('FIND_IN_SET(:city_ids, city_ids)');
    $criteria->params[':city_ids'] = $this->city_ids;
}

YII 判斷是否是Ajax請求,獲取請求引數

//yii 1 ,是否是post
if (Yii::app()->request->isPostRequest) {
    $key1 = Yii::app()->request->getParam('key1');
    echo 'ajax';
 }
//yii 1  是否是ajax
if (Yii::app()->request->isAjaxRequest) {
    echo 'ajax';
}
//yii2 是否是ajax
Yii::$app->request->isAjax

 YII1中模板建立URL

<?php echo Yii::app()->createUrl('/KnowledgeAuthUser/admin');?>

 YII1 checkbox

  <?php
        $auth_list = KnowledgeAuthList::getAllAuth();
        foreach ($auth_list as $key => $val) {
            $checked = in_array(strval($val['id']), $have_ids = isset($have_ids) ? $have_ids : [] ) ? true: false;
            echo CHtml::checkBox("KnowledgeAuthUser[auth_list][]", $checked, array("value" => $val['id'], 'class' => 'customer_level')) . $val['auth_name'] . '&nbsp;&nbsp;';
        }
        ?>

 YII1註冊引入JS擴充套件檔案(layer.js)

<?php
$assets = Yii::app()->request->hostInfo."/v2/sto/classic/www/js/layer/layer.js";
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile($assets);
?>

 YII1 fandall查詢指定欄位

modelName::model() -> find()    //找出的是一個物件
modelName::model() -> findALL()  //找出的是一個物件集合的陣列
$criteria = new CDbCriteria;
$criteria->select = 'username,id,email';
$criteria->order = 'id DESC';
$users = modelName::model()->findAll( $criteria );
$users = $this->user->find(array(
    'select'=>array('id','username','email'),
    'order' => 'id DESC',
    'condition' => 'state=:state AND id=:id',
    'params' => array(':state'=>'1',':id' => '2'),
));

 

 

 

 

 

相關文章