zoukankan      html  css  js  c++  java
  • Yii2.0 技巧总结

    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 untimelogs,Yii2 自带swiftmailer。我们可以通过配置修改Log的target。指定是以文本存储本地还是发送邮件。

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

    更多信息见 文档

        'components' => [
            'user' => [
                //.....
            ],
            'log' => [
                'traceLevel' => YII_DEBUG ? 3 : 0,
                'targets' => [
                    [
                        'class' => 'yiilogFileTarget',
                        'levels' => ['error', 'warning'],
                    ], 
                    [
                        'class' => 'yiilogEmailTarget',
                        'levels' => ['error'],
                        //'categories' => ['yiidb*'],
                        'message' => [
                            'from' => ['no_reply@qq.com'],
                            'to' => ['xx@qq.com'],
                            'subject' => basename(dirname(__DIR__)) .' errors in XX site',
                        ],
    'categories' => ['yiidb*', 'yiiwebHttpException:*'] // 只处理数据库和请求导致的错误
    'except' => ['yiiwebHttpException:404'], // 排除404,不然的话你会发现你的邮箱里全塞满了这些邮件
                    ],
                ],
            ],
            'mailer' => [
                'class' => 'yiiswiftmailerMailer',
                // 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


     

  • 相关阅读:
    区块链
    git在IDEA中的使用
    hadoop linux 杂记
    idea java web 使用说明
    克隆虚拟机,解决网卡问题
    最小化CentOS6.7(64bit)---安装mysql5.5、jdk、tomcat
    爬虫学习笔记(1)--环境准备与正则表达式
    图论 BZOJ 3669 [Noi2014]魔法森林
    Manacher Ural 1297 Palindrome
    动态规划,贪心 APIO 2015 Sculptures
  • 原文地址:https://www.cnblogs.com/mafeifan/p/4205527.html
Copyright © 2011-2022 走看看