'ID', 'coupon_id' => '优惠券id', 'goods_id' => '商品id', 'created_at' => '创建时间', 'updated_at' => '修改时间', ]; } /** * 保存数据 * @param $coupon_id * @param $goods_id * @return bool * @throws \yii\db\Exception */ public static function setData($coupon_id, $goods_id) { try { if ($coupon_id && $goods_id) { $count = self::deleteAll([ 'and', ['coupon_id' => $coupon_id], ['not in', 'goods_id', $goods_id] ]); $exist_goods_ids = self::find()->select('goods_id') ->where(['goods_id' => $goods_id,'coupon_id' => $coupon_id]) ->column(); $insert = []; $now = time(); foreach ($goods_id as $key => $id) { if (in_array($id, $exist_goods_ids)) continue; $insert[] = [$coupon_id, $id, $now, $now]; } $field = ['coupon_id', 'goods_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; } } }