Yii2速查表-持续更新中

小程府 发布于2017-08-25 15:24
  • 615 人浏览

Composer

  1. //linux下安装composer
  2. curl -sS https://getcomposer.org/installer | php
  3. mv composer.phar /usr/local/bin/composer
  4. //升级composer
  5. composer self-update
  6. //创建项目(安装yii程序)
  7. composer create-project yiisoft/yii2-app-advanced advanced
  8. //通过composer.json安装扩展
  9. composer install
  10. //更新本地composer扩展库
  11. composer update
  12. //直接安装某个composer扩展
  13. composerrequire[options][--][vendor/packages]...

main

  1. //时区设置
  2. 'timeZone' => 'Asia/Shanghai',
  3. //语言设置
  4. 'language' => 'zh-CN',
  5. //mysql设置
  6. 'db' => [
  7. 'class' => 'yii\db\Connection',
  8. 'dsn' => 'mysql:host=127.0.0.1;dbname=xcf',
  9. 'username' => 'root',
  10. 'password' => 'hp8805623',
  11. 'charset' => 'utf8',
  12. 'tablePrefix' => 'cf_',
  13. ],
  14. //redis设置
  15. 'redis' => [
  16. 'class' => 'yii\redis\Connection',
  17. 'hostname' => '127.0.0.1',
  18. 'port' => 6379,
  19. 'database' => 0,
  20. 'password' => 'RWynsfd',
  21. ],
  22. //mongodb设置
  23. //有账户的配置,username是用户名,password是密码,database是数据库
  24. 'mongodb' => [
  25. 'class' => 'yii\mongodb\Connection',
  26. 'dsn' => 'mongodb://127.0.0.1:27017/database',
  27. //'dsn' => 'mongodb://username:password@localhost:27017/database',
  28. ],
  29. //session设置
  30. 'session' => [
  31. 'class'=>'\yii\web\Session',
  32. 'name' => 'advanced-backend',
  33. 'cookieParams'=> [
  34. 'lifetime'=> 3600 * 24 * 30,
  35. ],
  36. //'savePath'=>Yii::getAlias('@backend').'/runtime/cache',
  37. ]
  38. //url路由常规设置
  39. 'urlManager' => [
  40. 'enablePrettyUrl' => true,
  41. 'showScriptName' => false,
  42. 'rules' => [
  43. '<module:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>'=>'<module>/<controller>/<action>',
  44. '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
  45. '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
  46. ],
  47. ],

Rules(validate)

  1. //必填项验证
  2. [['name','price'], 'required','message'=> "{attribute}不能为空"],
  3. [['price'],'number'],
  4. //过滤空格
  5. [['name'],'trim'],
  6. [[ 'status'],'integer'],
  7. [['addtime'],'safe'],
  8. [['name'],'string', 'max' => 80],
  9. //双精度
  10. [['price'],'double','max'=>10000],
  11. [['price'],'default','value'=>1],
  12. //验证最大值、最小值
  13. [['price'],'number','min'=>1,'max'=>10000],
  14. //自定义验证规则,命名为nameUnique
  15. [['name'],'nameUnique'],
  16. //插入时验证
  17. [['password','passwordRepeat'], 'required','on'=>'insert'],
  18. //正则验证
  19. ["username","match","pattern"=>"/^[a-z]\w*$/i"]
  20. [['password','passwordRepeat'], 'match', 'pattern' => '/^\S+$/'],
  21. 比较验证
  22. ['passwordRepeat','compare', 'compareAttribute' => 'password', 'message' => '两次密码不一致']
  23. //Email验证
  24. ["email",'email']
  25. [["email","work_email"],'email']
  26. //Boolean验证,可以认为置顶 true / false 值。
  27. ['sex', 'boolean', 'trueValue' => true, 'falseValue' => false, 'strict' => true];
  28. //captcha验证码
  29. ['verificationCode', 'captcha'];
  30. //compare比较,错误信息将提示给username
  31. ['username', 'compare', 'compareAttribute' => 'province','message'=>'username和province必须一样']
  32. //date验证
  33. ["birth","date","format"=>"Y-m-d"]
  34. //default验证
  35. ['age','default','value'=>'']
  36. //默认值扩展
  37. [['from','to'],'default','value'=>function($model,$attribute){return date('Y-m-d', strtotime($attribute === 'to' ? '+3 days' : '+6 days'));}]
  38. //数组各元素验证
  39. /* 要求验证的元素必须为数组,否则会返回假并报错*/
  40. ["categoryIds","each","rule"=>['integer']]
  41. //filter过滤验证函数
  42. [['username', 'email'], 'filter', 'filter' => 'trim', 'skipOnArray' => true],
  43. ['phone', 'filter', 'filter' => function($value){
  44. //image验证
  45. /* 上传png/jpg格式,最大宽度不能超过1000px,最小宽度不能低于100px,最大高度不能高于1000px。最小高度不能低于100px */
  46. ['primaryImage', 'image', 'extensions' => 'png, jpg','minWidth' => 100, 'maxWidth' => 1000,'minHeight' => 100, 'maxHeight' => 1000]
  47. //ip验证
  48. ["ip_addess","ip"]
  49. //in方法验证
  50. ["level","in","range"=>[1,2,3]]
  51. //integer验证
  52. ["age",'integer'];["age","integer","max"=>90,"min"=>1]
  53. //safe验证(多用于设置一个model的attribute)
  54. ["description","safe"]
  55. //string验证
  56. ["username","string","length"=>[4,24]],
  57. ["username","string","max"=>32],
  58. ["username","string","encoding"=>"UTF-8"];
  59. //unique唯一验证
  60. ["username","unique"]["username","unique","targetAttribute"=>"province"]
  61. //url验证
  62. ["website","url"],
  63. ["website","url","validSchemes"=>["http","https"]]

Db

  1. //获得数据库连接
  2. $conn = Yii::$app->db;
  3. //执行数据库查询语句
  4. Yii::$app->db->createCommand("SELECT * FROM `user`");
  5. Yii::$app->db->createCommand("SELECT * FROM `user` WHERE uid=:uid",[":uid"=>1]);
  6. //SQL语句插入数据
  7. Yii::$app->db
  8. ->createCommand('INSERT INTO user (email, password) VALUES("test3@example.com", "test3");')->execute();
  9. //数组形式插入数据
  10. Yii::$app->db->createCommand()->insert('user', [
  11. 'email' => 'test4@example.com',
  12. 'password' => 'changeme7',
  13. 'first_name' => 'Test'
  14. ])->execute();
  15. //批量插入数据
  16. Yii::$app->db->createCommand()->batchInsert('user', ['email', 'password', 'first_name'],
  17. [
  18. ['james.franklin@example.com', 'changeme7', 'James'],
  19. ['linda.marks@example.com', 'changeme7', 'Linda'],
  20. ['roger.martin@example.com', 'changeme7','Ualdsf']
  21. ])->execute();
  22. //更新数据
  23. Yii::$app->db->createCommand()->update('user', ['updated_at' => time()], 'id = 2')->execute();
  24. //删除数据
  25. Yii::$app->db->createCommand()->delete('user', 'id = 3')->execute();
  26. /*获取结果方法*/
  27. //获取所有数据(数组形式返回)
  28. Yii::$app->db->createCommand("SELECT * FROM `user`")->queryAll();
  29. //获取一条数据(一维数组)
  30. Yii::$app->db->cr

继续阅读

还有20%的精彩内容,登录继续阅读