| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace addons\Store\common\models;
- use common\models\base\BaseActiveRecord;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_store_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 StoreGoodsCateRelate extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_store_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' => '修改时间',
- ];
- }
- public static function setData($goods_id,$cats){
- try{
- if($goods_id && $cats){
- $count = self::deleteAll([
- 'and',
- ['goods_id' => $goods_id],
- ['not in','cate_id',$cats]
- ]);
- $exist_cate_ids = self::find()->select('cate_id')->where(array('goods_id'=>$goods_id))->column();
- $insert = [];
- $now = time();
- foreach($cats as $key => $cate_id){
- if(in_array($cate_id,$exist_cate_ids)) continue;
- $insert[] = [$goods_id,$cate_id,$now,$now];
- }
- $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;
- }
- }
- /**
- * 改变商品分类
- * @Author bing
- * @DateTime 2021-08-23 09:38:52
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param integer $before_cate_id
- * @param integer $after_cate_id
- * @return int/bool
- */
- public static function changeGoodsCate(int $before_cate_id, int $after_cate_id){
- try{
- $count = self::updateAll(
- ['cate_id' => $after_cate_id],
- ['cate_id' => $before_cate_id]
- );
- return $count;
- }catch(\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|