GoodsDescriptionTemplate.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace common\models\goods;
  3. use common\enums\StatusEnum;
  4. use Exception;
  5. use RuntimeException;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_goods_description_template".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城ID
  12. * @property string $name 模板名称
  13. * @property string $frontend_name 前端名称
  14. * @property int $use_num 使用该模板数量
  15. * @property string|null $fields 字段
  16. * @property int $status 状态[-1:删除;0:禁用;1启用]
  17. * @property int $created_at
  18. * @property int $updated_at
  19. */
  20. class GoodsDescriptionTemplate extends \common\models\base\BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return 'qimall_goods_description_template';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id', 'use_num', 'status', 'created_at', 'updated_at'], 'integer'],
  36. [['fields'], 'safe'],
  37. [['name'], 'string', 'max' => 8],
  38. [['frontend_name'], 'string', 'max' => 4],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'mall_id' => 'Mall ID',
  49. 'name' => 'Name',
  50. 'frontend_name' => 'Frontend Name',
  51. 'use_num' => 'Use Num',
  52. 'fields' => 'Fields',
  53. 'status' => 'Status',
  54. 'created_at' => 'Created At',
  55. 'updated_at' => 'Updated At',
  56. ];
  57. }
  58. /**
  59. * @param array $params
  60. *
  61. * @return bool|self
  62. */
  63. public static function setData(array $params)
  64. {
  65. try {
  66. if (!empty($params['id'])) {
  67. $model = static::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  68. if (!$model) {
  69. throw new RuntimeException('数据不存在或已删除');
  70. }
  71. if ($model->mall_id != $params['mall_id']) {
  72. throw new RuntimeException('不允许操作此数据');
  73. }
  74. } else {
  75. $model = new static();
  76. $model->loadDefaultValues();
  77. }
  78. if (!$model->load($params, '')) {
  79. throw new RuntimeException($model->getErrorMessage());
  80. }
  81. if (!$model->save()) {
  82. throw new RuntimeException($model->getErrorMessage());
  83. }
  84. return $model;
  85. } catch (Exception $e) {
  86. static::$static_error = $e->getMessage();
  87. return false;
  88. }
  89. }
  90. }