Search.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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;
  10. use yii\base\InvalidValueException;
  11. use yii\mongodb\ActiveRecord;
  12. /**
  13. * @author Terry Zhao <2358269014@qq.com>
  14. * @since 1.0
  15. */
  16. class Search extends ActiveRecord
  17. {
  18. /**
  19. * 语言,在使用model之前必须设置语言,否则报错。
  20. */
  21. public static $_lang;
  22. public static $_filterColumns;
  23. /**
  24. * mongodb collection 的名字,相当于mysql的table name
  25. */
  26. public static function collectionName()
  27. {
  28. if (self::$_lang) {
  29. return 'full_search_product_'.self::$_lang;
  30. } else {
  31. //throw new InvalidValueException('search class $_lang is empty, you must set search model class variable $_lang before use it');
  32. return 'full_search_product_no_lang';
  33. }
  34. }
  35. /**
  36. * mongodb是没有表结构的,因此不能像mysql那样取出来表结构的字段作为model的属性
  37. * 因此,需要自己定义model的属性,下面的方法就是这个作用
  38. */
  39. public function attributes()
  40. {
  41. $origin = [
  42. '_id',
  43. 'product_id',
  44. 'name',
  45. 'spu',
  46. 'sku',
  47. 'score',
  48. 'status',
  49. 'is_in_stock',
  50. 'url_key',
  51. 'price',
  52. 'cost_price',
  53. 'special_price',
  54. 'special_from',
  55. 'special_to',
  56. 'final_price', // 算出来的最终价格。这个通过脚本赋值。
  57. 'image',
  58. 'short_description',
  59. 'description',
  60. 'created_at',
  61. 'sync_updated_at', // 同步产品表信息到搜索表的时间戳。
  62. ];
  63. if (is_array(self::$_filterColumns) && !empty(self::$_filterColumns)) {
  64. $origin = array_merge($origin, self::$_filterColumns);
  65. $origin = array_unique($origin);
  66. }
  67. return $origin;
  68. }
  69. }