Yii2.0 技巧总结
View部分
1. 使用ActiveField中的hint生成提示文字
<?= $form->field($model, 'freightAddedFee')->textInput()->hint('大于0的整數(shù)') ?>?2. 文本框添加placeholder屬性,其實(shí)這個(gè)本來(lái)就是html5帶的屬性。
<?= $form->field($model, 'mobile', $input_class)->textInput(['maxlength' => 60,'placeholder' => '11位數(shù)字']) ?>3. ?用activeForm生成的元素不讓出現(xiàn)label
<?= $form->field($model, 'skuType1')->textInput()->label(false) ?>4. 使用GridView,如果數(shù)據(jù)庫(kù)中保存的是圖片地址,在前臺(tái)顯示成圖片可以使用format,并添加圖片樣式
['label' => '頭像','format' => ['image',['class' => 'thumbnail_image']],'value' => 'avatarUrl',],?
Controller 部分
1. 跳轉(zhuǎn)回上次的地址
return $this->redirect(Yii::$app->request->referrer);?
Model 部分
1. 通過(guò)中間表關(guān)聯(lián)查詢
public function getVendorNickName(){return $this->hasOne(User::className(), ['id' => 'userId']) ->viaTable(BaseVendor::tableName(), ['id' => 'vendorId']); }參見(jiàn):http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relations-with-junction-table
2. 查詢語(yǔ)句使用join時(shí)為表起別名。起別名可以防止沖突,因?yàn)橛袝r(shí)候會(huì)join一張表兩次。
下面的例子為user表起了u1的別名
$query->joinWith(['vendorNickName' => function ($q) {$q->where('u1.nickname LIKE "%' . $this->vendorNickName . '%"')->from(User::tableName().' u1');}]);生成的SQL類(lèi)似:
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 "%一號(hào)微店%"?
?配置和components
1. 1分鐘配置站點(diǎn)出錯(cuò)自動(dòng)發(fā)告警郵件功能。
我們知道站點(diǎn)出錯(cuò)時(shí),Yii2會(huì)記錄日志 可能會(huì)在存放在 \frontend\runtime\logs,Yii2 自帶swiftmailer。我們可以通過(guò)配置修改Log的target。指定是以文本存儲(chǔ)本地還是發(fā)送郵件。
具體見(jiàn)下面的代碼。你需要修改郵箱相關(guān)配置信息。
更多信息見(jiàn) 文檔
'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:*'] // 只處理數(shù)據(jù)庫(kù)和請(qǐng)求導(dǎo)致的錯(cuò)誤
'except' => ['yii\web\HttpException:404'], // 排除404,不然的話你會(huì)發(fā)現(xiàn)你的郵箱里全塞滿了這些郵件 ],],],'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
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/mafeifan/p/4205527.html
總結(jié)
以上是生活随笔為你收集整理的Yii2.0 技巧总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: (转载)WebSphere MQ安装过程
- 下一篇: GDOI2015 解题报告