| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- namespace addons\Purchase\common\models;
- use Yii;
- /**
- * This is the model class for table "{{%addons_purchase_goods_cate_relate}}".
- *
- * @property int $id
- * @property int|null $cate_id 分类id
- * @property int|null $goods_id 产品id
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class PurchaseGoodsCateRelate extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_purchase_goods_cate_relate}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['cate_id', 'goods_id', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'cate_id' => '分类id',
- 'goods_id' => '产品id',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- /**
- * 商品分类关联保存数据
- *
- * @param integer $goods_id
- * @param array $cats 分类ID
- * @param string $type
- * @return boolean
- */
- public static function setData(int $goods_id, array $cats, string $type = 'single')
- {
- try {
- if ($goods_id && $cats) {
- $insert = [];
- $now = time();
- // 删除旧的分类
- self::deleteAll([
- 'and',
- ['goods_id' => $goods_id],
- ]);
- // 生成新分类数据
- $insert = array_map(function ($item) use ($now, $goods_id) {
- return [$goods_id, $item, $now, $now];
- }, $cats);
- $field = ['goods_id', 'cate_id', 'created_at', 'updated_at'];
- // 批量插入数据
- Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
- }
- return true;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|