1、YII结构大概分为:MVC、Yii、入口脚本、应用、应用组件、模块、过滤器、小部件等;
2、ID:应用ID、模块ID、控制器ID、动作ID;
3、模型(models)中rules自定义验证规则:【Validator.php】
系统默认的验证规则:
boolean : CBooleanValidator 的别名, 确保属性的值是CBooleanValidator::trueValue 或 CBooleanValidator::falseValue .
captcha : CCaptchaValidator 的别名,确保了特性的值等于 CAPTCHA 显示出来的验证码.
compare : CCompareValidator 的别名, 确保了特性的值等于另一个特性或常量.
email : CEmailValidator 的别名,确保了特性的值是一个有效的电邮地址.
default : CDefaultValueValidator 的别名, 为特性指派了一个默认值.
exist : CExistValidator 的别名, 确保属性值存在于指定的数据表字段中.
file : CFileValidator 的别名, 确保了特性包含了一个上传文件的名称.
filter : CFilterValidator 的别名, 使用一个filter转换属性.
in : CRangeValidator 的别名, 确保了特性出现在一个预订的值列表里.
length : CStringValidator 的别名, 确保了特性的长度在指定的范围内.
match : CRegularExpressionValidator 的别名, 确保了特性匹配一个正则表达式.
numerical : CNumberValidator 的别名, 确保了特性是一个有效的数字.
required : CRequiredValidator 的别名, 确保了特性不为空.
type : CTypeValidato日期选择器小部件r 的别名, 确保了特性为指定的数据类型.
unique : CUniqueValidator 的别名, 确保了特性在数据表字段中是唯一的.
url : CUrlValidator 的别名, 确保了特性是一个有效的路径.
4、当进行系统更新时,要将所有请求默认到一个控制器和动作,可在配置文件中配置catchAll路由(全拦截路由)。
5、带附件发送邮件
/**
* 带附件立即发送邮件
* @access public
* @author gengpengshuai
* @param $address = array() 收件人
* @param $subject 邮件主题
* @param $body 内容主体
* @param $attachments 附件(存放路径) '/tmp/tmp/tmp.zip'
* @param $cc 抄送
* @param $bcc 秘密抄送
* @return
* @time 2018-05-09
*/
public static function sendMail($address, $subject, $body, $attachments, $cc = array(), $bcc = array()) {
array_push($cc, 'gengpengshuai@163.com');
$mail = \Yii::$app->mailer->compose();
$mail->setTo($address);
$mail->setCc($cc);
$mail->setBcc($bcc);
$file_con = file_get_contents($attachments);
$mail->attachContent($file_con,
array(
'filename'=> 'tmp.zip',
'contentType'=> 'application/x-zip-compressed'
)
);
$mail->setSubject($subject);
$mail->setHtmlBody($body);
try {
if($mail->send()) {
return true;
}else{
\Yii::info("nowSendMail error path:$attachments");
return false;
}
} catch (\Exception $e) {
\Yii::info(sprintf('mail send . err: %s', $e->getMessage()));
return false;
}
}
6、定时文件异步挂载进程接收参数
① cmd挂载命令: php yii file/entrance zhangsanfeng china >/dev/null 2>&1 &
② 脚本文件:FileController.php
<?php
namespace app\commands;
use Yii;
use yii\console\Controller;
use yii\db\Exception;
class FileController extends Controller{
public $name;
public $country;
/**
* 指定用于控制台的选项
* @access public
* @author
* @param
* @return
* @time 2018-05-09
*/
public function options($actionID){
return [
'name',
'country',
];
}
/**
* 程序入口
* @access public
* @author
* @param
* @return
* @time 2018-10-15
*/
public function actionEntrance($name, $country){
\Yii::info("fileController accept params:$name/$country");
}
}
7、Yii::$app使用:
① Yii获取Post/Get参数:Yii::$app->request->queryParams;
8、Yii2查询select给数据表中不存在的字段赋值:
use yii\db\Expression;
$select = new Expression("'a' as b");
$query = self::find()->select($select);
9、Yii2查询之where条件拼装:
熟悉Yii2的查询条件后,用Active Record查询数据非常方便;
以下我们介绍where()方法当中,条件的拼装方式:
#某个值为null,会用IS NULL来生成语句:
['type' => 1, 'status' => 2] // 生成:(type = 1) AND (status = 2)
['id' => [1, 2, 3], 'status' => 2] // 生成:(id IN (1, 2, 3)) AND (status = 2)
['status' => null] // 生成:status IS NULL
['NOT', ['type' => null]] // 生成:type IS NOT NULL
#对比
['>', 'id', 1] // 生成:id > 1
['<', 'id', 100] // 生成:id < 100
['=', 'id', 10] // 生成:id = 10
['>=', 'id', 1] // 生成:id >= 1
['<=', 'id', 100] // 生成:id <= 100
['!=', 'id', 10] // 生成:id != 10
['and', 'id' => 1, 'id' => 2] // 生成:id=1 AND id=2
['and', 'id=1', 'id=2'] // 生成:id=1 AND id=2
['and', 'type=1', ['or', 'id=1', 'id=2']] // 生成:type=1 AND (id=1 OR id=2)
['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]]] // 生成:(type IN (7, 8, 9) OR (id IN (1, 2, 3)))
['not', ['attribute' => null]] // 生成:NOT (attribute IS NULL)
['between', 'id', 1, 10] // 生成:id BETWEEN 1 AND 10
['not between', 'id', 1, 10] // 生成:id NOT BETWEEN 1 AND 10
['in', 'id', [1, 2, 3]] // 生成:id IN (1, 2, 3)
['id' => [4, 8, 15]] // 生成:id IN (4, 8, 15)
['not in', 'id', [1, 2, 3]] // 生成:id NOT IN (1, 2, 3)
['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]] // 生成:(`id`, `name`) IN ((1, 'foo'), (2, 'bar'))
#用子查询作为IN条件的值,如下:
['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]
['like', 'name', 'tester'] // 生成:name LIKE '%tester%'
['like', 'name', ['test', 'sample']] // 生成:name LIKE '%test%' AND name LIKE '%sample%'
['like', 'name', '%tester', false] // 生成:name LIKE '%tester'
// 这是自定义查询方式,要传入值为false的运算数3,并且自行添加%
['or like', 'name', ['test', 'sample']] // 生成:name LIKE '%test%' OR name LIKE '%sample%'
['not like', 'name', 'tester'] // 生成:name NOT LIKE '%tester%'
['or not like', 'name', ['test', 'sample']] // 生成:name NOT LIKE '%test%' OR name NOT LIKE '%sample%'
['exists', (new Query())->select('id')->from('users')->where(['active' => 1])] // 生成:EXISTS (SELECT "id" FROM "users" WHERE "active"=1)