UrlRewrite.php 1.5 KB

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