虽然初学不太会什么,但是真的很喜欢YII [ 技术分享 ]
(1)use yii\bootstrap\ActiveForm;
请求form表单
<?php $form = ActiveForm::begin(['id' => '', 'action' => "提交地址" . $model->id]); ?>
<?=
$form->field($order, '已实例化的字段', [
'inputOptions' => ['class' => '', 'placeholder' => '', 'tabindex' => ''],
'template' => '<div class="">{input}</div>
<div class="">{error}</div>
'])->textInput();
radioList(['0' => '冻结', '1' => '正常']);
textarea();
?>
<?php ActiveForm::end(); ?>
(2)use yii\data\Pagination;
请求分页
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => '20']);
$model = $data->offset($pages->offset)->limit($pages->limit)->orderBy('id asc')->asArray()->all();
<?= LinkPager::widget(['pagination' => $pages]); ?>
(3)foreach循环
<?php foreach ($model as $val) { ?><?php } ?>
<?php foreach ($power as $key => $val) { ?><?php } ?>
(4)$this->render(,[''])
渲染页面 $this->redirect(['']);
重定向
(5)实例化对象
$model = Models::find();
$model = Models::findOne(' '=>' ',' '=>' ');
$model = $id ? Models::findOne($id) : new Models();
(6)类的公用函数指定布局
public $layout = 'view';
(7)查询条件
where([' '=>' ',' '=>' '])查询条件
select(' ')查询字段
asArray( )查询结果作为数组
(8)控制器接收数据
$model->load(Yii::$app->request->post()) && $model->validate()
$params = Yii::$app->request->post();
$params = Yii::$app->request->get();
(9)函数
$newfield = explode(',', $abc); 把字符串按照参数分割为数组
number_format(number,decimals,decimalpoint,separator) 函数通过千位分组来格式化数字。
bcadd()
bcsub()
(10)ajax传参数json
use yii\web\Response;
public function init() init初始化的成员函数使用
{
parent::init();
if (Yii::$app->request->isAjax)
Yii::$app->response->format = Response::FORMAT_JSON;
}
return ['msg'=>'提示信息'];
(11)保存数据
$_model->save();
(12)引入文件
$this->registerCssFile('相对地址', ['depends' => 'yii\web\YiiAsset', 'position' => 1]);
$this->registerJsFile('相对地址', ['depends' => 'yii\web\YiiAsset', 'position' => 1]);
(13)用户是否登录
if (\Yii::$app->user->isGuest) {
return $this->redirect('/');
}
$id = Yii::$app->user->getIdentity()->id; 获取当前用户的id
(14)联查
$record = 表一::find()->innerJoin(表二, $order_table . ".online_pid=" . $product_table . ".id")
->select($order_table . ".*," . $product_table . ".title as abc,"" . $product_table . '.money,' . $product_table . '.yield_rate,' . $product_table . '.refund_method,' . $product_table . '.expires,' . $product_table . '.start_date,' . $product_table . '.end_date')
$sql = $record->createCommand()->getRawSql();
$model = $db->createCommand($sql)->queryAll();
(15)报错
$model->addError('字段', '提示');
(16)事物
$transaction = Yii::$app->db->beginTransaction();
$transaction->commit();
$transaction->rollBack();
(17)隐藏域
<?= $form->field($omodel, 'order_money', ['template' => '{input}'])->hiddenInput() ?>
<input type="hidden" value="<?= $omodel->order_money ?>" name="order_money"/>
<?= $form->field($omodel, 'order_money', ['template' => '{error}']) ?>
共 5 条回复
倾城龙爱
注册时间:2015-03-02
最后登录:2018-03-23
在线时长:13小时27分
最后登录:2018-03-23
在线时长:13小时27分
- 粉丝8
- 金钱135
- 威望0
- 积分265