Yii笔记:打印sql、Form表单、时间插件、Mysql的 FIND_IN_SET函数使用、是否是post/ajax请求
语句部分:
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'] . ' ';
}
?>
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'), ));
echo Yii::app()->request->redirect('http://www.baidu.com');