AttrLibrary.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use yii\behaviors\TimestampBehavior;
  9. use yii\db\ActiveRecord;
  10. /**
  11. * Class AttrLibrary
  12. * @package app\modules\common\models
  13. * @property integer $id
  14. * @property integer $store_id
  15. * @property integer $parent_id
  16. * @property string $name
  17. * @property integer $sort
  18. * @property integer $is_delete
  19. * @property string $type
  20. * @property string $created_at
  21. * @property string $updated_at
  22. */
  23. class AttrLibrary extends ActiveRecord
  24. {
  25. /**
  26. * 类型
  27. */
  28. const TYPE_0 = 0;
  29. /**
  30. * 规格
  31. */
  32. const TYPE_1 = 1;
  33. /**
  34. * 属性
  35. */
  36. const TYPE_2 = 2;
  37. public function behaviors()
  38. {
  39. return [
  40. [
  41. // 自动更新创建和更新时间
  42. 'class' => TimestampBehavior::class,
  43. 'value' => time()
  44. ]
  45. ];
  46. }
  47. public static function tableName()
  48. {
  49. return '{{%attr_library}}';
  50. }
  51. public function rules()
  52. {
  53. return [
  54. [['store_id', 'name',], 'required'],
  55. [['store_id', 'parent_id', 'sort', 'created_at', 'updated_at', 'is_delete', 'type'], 'integer'],
  56. [['name'], 'string', 'max' => 255],
  57. ];
  58. }
  59. public function attributeLabels()
  60. {
  61. return [
  62. 'id' => 'ID',
  63. 'store_id' => '商城id',
  64. 'parent_id' => '上级分类id',
  65. 'name' => '分类名称',
  66. 'sort' => '排序,升序',
  67. 'created_at' => '创建时间',
  68. 'updated_at' => '更新时间',
  69. 'is_delete' => 'Is Delete',
  70. 'type' => '类型',
  71. ];
  72. }
  73. /**
  74. * 获取所有的分类
  75. * @param int $store_id
  76. * @param int $parent_id
  77. * @return array
  78. */
  79. public static function getList($store_id = 1, $parent_id = 0)
  80. {
  81. $res = [];
  82. $query = self::find()->where([
  83. 'parent_id' => $parent_id,
  84. 'is_delete' => 0,
  85. 'store_id' => $store_id
  86. ])->orderBy(['sort'=>SORT_DESC]);
  87. $list = $query->asArray()->all();
  88. if (empty($list)) {
  89. return $res;
  90. }
  91. foreach ($list as &$val) {
  92. $val['children'] = self::getList($store_id, $val['id']);
  93. $res[] = $val;
  94. }
  95. return $res;
  96. }
  97. }