Article.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\cms;
  10. use yii\mongodb\ActiveRecord;
  11. /**
  12. * @author Terry Zhao <2358269014@qq.com>
  13. * @since 1.0
  14. */
  15. class Article extends ActiveRecord
  16. {
  17. const STATUS_DELETED = 10;
  18. const STATUS_ACTIVE = 1;
  19. /**
  20. * mongodb collection 的名字,相当于mysql的table name
  21. */
  22. public static function collectionName()
  23. {
  24. return 'article';
  25. }
  26. /**
  27. * mongodb是没有表结构的,因此不能像mysql那样取出来表结构的字段作为model的属性
  28. * 因此,需要自己定义model的属性,下面的方法就是这个作用
  29. */
  30. public function attributes()
  31. {
  32. return [
  33. '_id',
  34. 'url_key',
  35. 'title',
  36. 'meta_keywords',
  37. 'meta_description',
  38. 'content',
  39. 'status',
  40. 'created_at',
  41. 'updated_at',
  42. 'created_user_id',
  43. ];
  44. }
  45. /**
  46. * 给model对应的表创建索引的方法
  47. * 在indexs数组中填写索引,如果有多个索引,可以填写多行
  48. * 在migrate的时候会运行创建索引,譬如:
  49. * @fecshop/migrations/mongodb/m170228_072455_fecshop_tables
  50. */
  51. public static function create_index()
  52. {
  53. $indexs = [
  54. ['url_key' => -1],
  55. ];
  56. $options = ['background' => true];
  57. foreach ($indexs as $columns) {
  58. self::getCollection()->createIndex($columns, $options);
  59. }
  60. }
  61. }