| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use Yii;
- /**
- * This is the model class for table "{{%mch_goods_cat}}".
- *
- * @property integer $id
- * @property integer $goods_id
- * @property integer $cat_id
- */
- class MchGoodsCat extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%mch_goods_cat}}';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['goods_id', 'cat_id'], 'required'],
- [['goods_id', 'cat_id'], 'integer'],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'goods_id' => 'Goods ID',
- 'cat_id' => 'Cat ID',
- ];
- }
-
- public static function saveCat($goods_id, $mch_cat_id) {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if ($mch_cat_id && count($mch_cat_id)) {
- if ($goods_id > 0) {
- MchGoodsCat::deleteAll(['goods_id' => $goods_id]);
- }
- $mchGoodsCatForm = new MchGoodsCat();
- foreach ($mch_cat_id as $cat_id) {
- $mchGoodsCat = clone $mchGoodsCatForm;
- $mchGoodsCat->goods_id = $goods_id;
- $mchGoodsCat->cat_id = $cat_id;
- if (!$mchGoodsCat->save()) {
- throw new \Exception('入驻商分类保存失败。' . array_shift($mchGoodsCat->getFirstErrors()));
- }
- }
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => 'success',
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|