You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.7 KiB

  1. <?php
  2. namespace backend\modules\goods\models\ars;
  3. use Yii;
  4. use yii\behaviors\TimestampBehavior;
  5. use backend\modules\goods\models\ars\Category;
  6. /**
  7. * This is the model class for table "atg_attribute".
  8. *
  9. * @property int $id
  10. * @property string $name 属性名
  11. * @property string $value 属性值
  12. * @property int $type 类型
  13. * @property int $sort_order 排序
  14. * @property int $is_delete 是否删除,1为已删除
  15. * @property int $created_at 创建时间
  16. * @property int $updated_at 更新时间
  17. * @property int $cat_id 后台商品分类id
  18. */
  19. class Attribute extends \yii\db\ActiveRecord
  20. {
  21. //是否删除is_delete
  22. const IS_DELETE_NO = 0;//未删除
  23. const IS_DELETE_YES = 1;//已删除
  24. //类型type
  25. const TYPE_ATTR = 1;//商品sku属性
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return 'atg_attribute';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['name', 'value'], 'required'],
  40. [['value'], 'string'],
  41. [['type', 'sort_order', 'is_delete', 'cat_id'], 'integer'],
  42. [['name'], 'string', 'max' => 50],
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'id' => 'id',
  52. 'name' => '规格名称',
  53. 'value' => '规格内容(每项以逗号隔开)',
  54. 'type' => '类型',
  55. 'sort_order' => '排序',
  56. 'is_delete' => '是否删除,1为已删除',
  57. 'created_at' => '创建时间',
  58. 'updated_at' => '更新时间',
  59. 'cat_id' => '后台商品分类',
  60. ];
  61. }
  62. /**
  63. * @author linyao
  64. * @email 602604991@qq.com
  65. * @created Nov 8, 2019
  66. *
  67. * 行为存储创建时间和更新时间
  68. */
  69. public function behaviors()
  70. {
  71. return [
  72. [
  73. 'class' => TimestampBehavior::className(),
  74. 'createdAtAttribute' => 'created_at',
  75. 'updatedAtAttribute' => 'updated_at',
  76. 'value' => function() {
  77. return time();
  78. },
  79. ],
  80. ];
  81. }
  82. /**
  83. * @return array
  84. * 数据键值对
  85. */
  86. public static function modelColumn()
  87. {
  88. return $column = self::find()->select(['name'])->where(['is_delete' => self::IS_DELETE_NO])->indexBy('id')->column();
  89. }
  90. public function beforeSave($insert)
  91. {
  92. if(!$this->type){
  93. $this->type = self::TYPE_ATTR;
  94. }
  95. return parent::beforeSave($insert); // TODO: Change the autogenerated stub
  96. }
  97. public function getCategory()
  98. {
  99. return $this->hasOne(Category::className(), ['id' => 'cat_id']);
  100. }
  101. }