MchGoodsCat.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. /**
  10. * This is the model class for table "{{%mch_goods_cat}}".
  11. *
  12. * @property integer $id
  13. * @property integer $goods_id
  14. * @property integer $cat_id
  15. */
  16. class MchGoodsCat extends \yii\db\ActiveRecord
  17. {
  18. /**
  19. * @inheritdoc
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%mch_goods_cat}}';
  24. }
  25. /**
  26. * @inheritdoc
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['goods_id', 'cat_id'], 'required'],
  32. [['goods_id', 'cat_id'], 'integer'],
  33. ];
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function attributeLabels()
  39. {
  40. return [
  41. 'id' => 'ID',
  42. 'goods_id' => 'Goods ID',
  43. 'cat_id' => 'Cat ID',
  44. ];
  45. }
  46. public static function saveCat($goods_id, $mch_cat_id) {
  47. $t = \Yii::$app->db->beginTransaction();
  48. try {
  49. if ($mch_cat_id && count($mch_cat_id)) {
  50. if ($goods_id > 0) {
  51. MchGoodsCat::deleteAll(['goods_id' => $goods_id]);
  52. }
  53. $mchGoodsCatForm = new MchGoodsCat();
  54. foreach ($mch_cat_id as $cat_id) {
  55. $mchGoodsCat = clone $mchGoodsCatForm;
  56. $mchGoodsCat->goods_id = $goods_id;
  57. $mchGoodsCat->cat_id = $cat_id;
  58. if (!$mchGoodsCat->save()) {
  59. throw new \Exception('入驻商分类保存失败。' . array_shift($mchGoodsCat->getFirstErrors()));
  60. }
  61. }
  62. }
  63. $t->commit();
  64. return [
  65. 'code' => 0,
  66. 'msg' => 'success',
  67. ];
  68. } catch (\Exception $e) {
  69. $t->rollBack();
  70. \Yii::error($e);
  71. return [
  72. 'code' => 1,
  73. 'msg' => $e->getMessage()
  74. ];
  75. }
  76. }
  77. }