GoodsTemplateRelation.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace common\models\goods\template;
  3. use common\enums\StatusEnum;
  4. /**
  5. * This is the model class for table "{{%goods_template_relation}}".
  6. *
  7. * @property int $id
  8. * @property int $mall_id 商城ID
  9. * @property int $goods_id Goods ID
  10. * @property int $template_id 模板ID[qimall_goods_decoration_template主键]
  11. * @property int $status 状态【1启用 0禁用 -1删除】
  12. * @property int $created_at 创建时间
  13. * @property int $updated_at 更新时间
  14. * @property GoodsDecorationTemplate $template
  15. */
  16. class GoodsTemplateRelation extends \common\models\base\BaseActiveRecord
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%goods_template_relation}}';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['mall_id', 'goods_id', 'template_id', 'status', 'created_at', 'updated_at'], 'integer'],
  32. ];
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function attributeLabels()
  38. {
  39. return [
  40. 'id' => 'ID',
  41. 'mall_id' => '商城ID',
  42. 'goods_id' => 'Goods ID',
  43. 'template_id' => '模板ID[qimall_goods_decoration_template主键]',
  44. 'status' => '状态【1启用 0禁用 -1删除】',
  45. 'created_at' => '创建时间',
  46. 'updated_at' => '更新时间',
  47. ];
  48. }
  49. public function getTemplate()
  50. {
  51. return $this->hasOne(GoodsDecorationTemplate::class, ['id' => 'template_id']);
  52. }
  53. /**
  54. * 保存数据
  55. * @Author bing
  56. * @DateTime 2021-08-26 14:16:24
  57. * @copyright: Copyright (c) 2020 广东七件事集团
  58. * @param array $params
  59. * @return object|boolean
  60. */
  61. public static function setData(array $params)
  62. {
  63. try {
  64. $model = self::findOne(['goods_id' => $params['goods_id'], 'mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  65. if(empty($model)){
  66. $model = new self();
  67. $model->loadDefaultValues();
  68. }
  69. if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
  70. return $model;
  71. } catch (\Exception $e) {
  72. self::$static_error = $e->getMessage();
  73. return false;
  74. }
  75. }
  76. }