View部分

1. 使用ActiveField中的hint生成提示文字

<?= $form->field($model, 'freightAddedFee')->textInput()->hint('大于0的整数') ?>

2. 文本框添加placeholder属性,其实这个本来就是html5带的属性。

<?= $form->field($model, 'mobile', $input_class)->textInput(['maxlength' => 60,'placeholder' => '11位数字']) ?>

3.  用activeForm生成的元素不让出现label

<?= $form->field($model, 'skuType1')->textInput()->label(false) ?>

4. 使用GridView,如果数据库中保存的是图片地址,在前台显示成图片可以使用format,并添加图片样式

            [
'label' => '头像',
'format' => ['image',['class' => 'thumbnail_image']],
'value' => 'avatarUrl',
],

Controller 部分

1. 跳转回上次的地址

return $this->redirect(Yii::$app->request->referrer);

Model 部分

1. 通过中间表关联查询

    public function getVendorNickName(){
return $this->hasOne(User::className(), ['id' => 'userId'])
->viaTable(BaseVendor::tableName(), ['id' => 'vendorId']);
}

参见:http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relations-with-junction-table

2. 查询语句使用join时为表起别名。起别名可以防止冲突,因为有时候会join一张表两次。

下面的例子为user表起了u1的别名

$query->joinWith(['vendorNickName' => function ($q) {
$q->where('u1.nickname LIKE "%' . $this->vendorNickName . '%"')
->from(User::tableName().' u1');
}]);

生成的SQL类似:

SELECT `za_order`.* FROM `za_order`
LEFT JOIN `za_user_vendor`
ON `za_order`.`vendorId` = `za_user_vendor`.`id`
LEFT JOIN `za_user` `u1`
ON `za_user_vendor`.`userId` = `u1`.`id`
WHERE u1.nickname LIKE "%一号微店%"

 配置和components

1. 1分钟配置站点出错自动发告警邮件功能。

我们知道站点出错时,Yii2会记录日志 可能会在存放在 \frontend\runtime\logs,Yii2 自带swiftmailer。我们可以通过配置修改Log的target。指定是以文本存储本地还是发送邮件。

具体见下面的代码。你需要修改邮箱相关配置信息。

更多信息见 文档

    'components' => [
'user' => [
//.....
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
[
'class' => 'yii\log\EmailTarget',
'levels' => ['error'],
//'categories' => ['yii\db\*'],
'message' => [
'from' => ['no_reply@qq.com'],
'to' => ['xx@qq.com'],
'subject' => basename(dirname(__DIR__)) .' errors in XX site',
],
'categories' => ['yii\db\*', 'yii\web\HttpException:*'] // 只处理数据库和请求导致的错误
'except' => ['yii\web\HttpException:404'], // 排除404,不然的话你会发现你的邮箱里全塞满了这些邮件
                ],
],
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// viewPath 不使用
'viewPath' => '',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.qq.com',
'username' => 'xxx@qq.com',
'password' => 'your_password',
'port' => '465',
'encryption' => 'ssl',
],
],

参考:http://www.kkh86.com/it/yii2-adv/guide-base-modify-jquery.html

最新文章

  1. Kali Linux Web 渗透测试视频教程—第十四课-arp欺骗、嗅探、dns欺骗、session劫持
  2. [Ogre][地形][原创]基于OgreTerrain的地形实现
  3. 【自动化测试】Selenium的智能等待
  4. Unable to execute dex: method ID not in [0, 0xffff]: 65536
  5. Tab选项卡切换卡JS效果
  6. Jquery基础之ajax
  7. weather compare
  8. IOS-Plist文件存储(1)
  9. 【转】height,posHeight和pixelHeight区别
  10. linux下安编译安装redis
  11. Stm32 GPIO复习
  12. 手把手丨我们在UCL找到了一个糖尿病数据集,用机器学习预测糖尿病(三)
  13. Vue组件之间数据交互与通信
  14. 关于ashrpt中行源的CPU + Wait for CPU事件深入解读
  15. day06字典类型
  16. ping -c 3 localhost
  17. HTTP.ResponseCode
  18. 【LeetCode 235_二叉搜索树】Lowest Common Ancestor of a Binary Search Tree
  19. java中需要用equals来判断两个字符串值是否相等
  20. td内的所有数字格式化保留两位小数

热门文章

  1. 从getshell到提权
  2. virtualbox+vagrant学习-2(command cli)-1-vagrant box命令
  3. 用c写一个小的聊天室程序
  4. CC2540 低功耗串口, POWER_SAVING 模式 下 串口 0 的使用
  5. php版本低更换php版本-问题以解决
  6. CSS节选——选择器
  7. 梯度下降法&amp;牛顿法
  8. oracle数据库的配置文件
  9. ABAP术语-Update Task
  10. 基于 HTML5 Canvas 的拓扑组件开发