Guest User

Untitled

a guest
Jul 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. # 配合数据库工作 Working With Databases
  2.  
  3. ## 数据库访问
  4.  
  5. ### DAO (Database Access Object)
  6.  
  7. 建立在 PHP PDO 基础上的抽象层
  8.  
  9. ## 查询生成器
  10.  
  11. ## 活动记录
  12.  
  13. * 用于访问和操作数据库中的数据
  14. * 与数据库表相关联
  15. * 一个 Active Record 实例对应一条数据库记录
  16. * Active Record 实例的属性对于数据库记录中的字段值
  17. * 免去直接编写 SQL 语句
  18.  
  19. ```php
  20. // 使用 Active Record
  21. $customer = new Customer();
  22. $customer->name = 'Qiang';
  23. $customer->save();
  24.  
  25. // 对于 MySQL, 等效于编写 SQL 语句
  26. $db->createCommand('INSERT INTO `customer` (`name`) VALUES (:name)', [
  27. ':name' => 'Qiang'
  28. ])->execute();
  29. ```
  30.  
  31. 可支持以下关系数据库:
  32.  
  33. * MySQL >= 4.1
  34. * PostgreSQL >= 7.3
  35. * SQLite 2 & SQLite 3
  36. * Microsoft SQL Server >= 2008
  37. * Oracle
  38. * CUBRID >= 9.3
  39. * Sphinx
  40. * ElasticSearch
  41.  
  42. 还支持以下 NoSQL 数据库:
  43.  
  44. * Redis >= 2.6.12
  45. * MongoDB >= 1.3.0
  46.  
  47.  
  48. ### 声明一个 Active Record 类
  49.  
  50. ```php
  51. namespace app\models;
  52.  
  53. use yii\db\ActiveRecord;
  54.  
  55. class Customer extends ActiveRecord
  56. {
  57. // 默认使用类名生成
  58. public static function tableName()
  59. {
  60. return '{{%customer}}';
  61. }
  62.  
  63. // 默认使用 config/main.php 配置中的 db 组件
  64. public static function getDb()
  65. {
  66. return \Yii::$app->db2;
  67. }
  68. }
  69. ```
  70.  
  71. ## 数据库迁移
  72.  
  73. ## Sphinx
  74.  
  75. ## Redis (yii2-redis)
  76.  
  77. ## MongoDB
  78.  
  79. ## ElasticSearch
Add Comment
Please, Sign In to add comment