AddonsCourseBanners.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace addons\Course\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%addons_course_banners}}".
  7. *
  8. * @property int $id
  9. * @property int $mall_id
  10. * @property int $sort 序号
  11. * @property string $title 图片标题
  12. * @property string $pic_link 图片跳转链接
  13. * @property string $pic_path 图片保存路径
  14. * @property int $is_show 是否展示,1:是,0:不是
  15. * @property int $status 活动状态,1:启用,0:禁用,-1删除
  16. * @property int $created_at
  17. * @property int $updated_at
  18. */
  19. class AddonsCourseBanners extends \common\models\base\BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%addons_course_banners}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['mall_id'], 'required'],
  35. [['mall_id', 'sort', 'is_show', 'status', 'created_at', 'updated_at'], 'integer'],
  36. [['title', 'pic_path'], 'string', 'max' => 255],
  37. ['pic_link', 'string', 'max' => 1000],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => 'Mall ID',
  48. 'sort' => '序号',
  49. 'title' => '图片标题',
  50. 'pic_link' => '图片跳转链接',
  51. 'pic_path' => '图片保存路径',
  52. 'is_show' => '是否展示,1:是,0:不是',
  53. 'status' => '活动状态,1:启用,0:禁用,-1删除',
  54. 'created_at' => 'Created At',
  55. 'updated_at' => 'Updated At',
  56. ];
  57. }
  58. public static function setData(int $mall_id, array $data)
  59. {
  60. if (!empty($data['pic_link']) && is_array($data['pic_link'])) {
  61. $data['pic_link'] = json_encode($data['pic_link']);
  62. }
  63. if (!empty($data['id'])) {
  64. $model = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]);
  65. if (empty($model)) {
  66. self::$static_error = '数据异常';
  67. return false;
  68. }
  69. } else {
  70. $model = new self();
  71. $model->loadDefaultValues();
  72. $model->mall_id = $mall_id;
  73. $model->status = StatusEnum::ENABLED;
  74. }
  75. foreach ($data as $key => $val) {
  76. $model->$key = $val;
  77. }
  78. if (!$model->save()) {
  79. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  80. return false;
  81. }
  82. return $model;
  83. }
  84. }