Newsletter.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\models\mongodb\customer;
  10. use yii\mongodb\ActiveRecord;
  11. /**
  12. * @author Terry Zhao <2358269014@qq.com>
  13. * @since 1.0
  14. */
  15. class Newsletter extends ActiveRecord
  16. {
  17. const ENABLE_STATUS = 1;
  18. const DISABLE_STATUS = 10;
  19. /**
  20. * mongodb collection 的名字,相当于mysql的table name
  21. */
  22. public static function collectionName()
  23. {
  24. return 'newsletter';
  25. }
  26. /**
  27. * mongodb是没有表结构的,因此不能像mysql那样取出来表结构的字段作为model的属性
  28. * 因此,需要自己定义model的属性,下面的方法就是这个作用
  29. */
  30. public function attributes()
  31. {
  32. return [
  33. '_id',
  34. 'email',
  35. 'created_at',
  36. 'status',
  37. ];
  38. }
  39. public static function primaryKey()
  40. {
  41. return '_id';
  42. }
  43. /**
  44. * 给model对应的表创建索引的方法
  45. * 在indexs数组中填写索引,如果有多个索引,可以填写多行
  46. * 在migrate的时候会运行创建索引,譬如:
  47. * @fecshop/migrations/mongodb/m170228_072455_fecshop_tables
  48. */
  49. public static function create_index()
  50. {
  51. $indexs = [
  52. ['email' => -1],
  53. ];
  54. $options = ['background' => true];
  55. foreach ($indexs as $columns) {
  56. self::getCollection()->createIndex($columns, $options);
  57. }
  58. }
  59. }