AddonsCouponCatRelate.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace addons\Coupon\common\models;
  3. use common\models\base\BaseActiveRecord;
  4. use PHPUnit\Util\Exception;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_coupon_cat_relate".
  8. *
  9. * @property int $id
  10. * @property int $coupon_id 优惠券id
  11. * @property int $cate_id 分类id
  12. * @property int $created_at 创建时间
  13. * @property int $updated_at 修改时间
  14. */
  15. class AddonsCouponCatRelate extends BaseActiveRecord
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public static function tableName()
  21. {
  22. return '{{%addons_coupon_cat_relate}}';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function rules()
  28. {
  29. return [
  30. [['coupon_id', 'cate_id'], 'required'],
  31. [['coupon_id', 'cate_id', 'created_at', 'updated_at'], 'integer'],
  32. ];
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function attributeLabels()
  38. {
  39. return [
  40. 'id' => 'ID',
  41. 'coupon_id' => '优惠券id',
  42. 'cate_id' => '分类id',
  43. 'created_at' => '创建时间',
  44. 'updated_at' => '修改时间',
  45. ];
  46. }
  47. /**
  48. * 保存数据
  49. * @param $coupon_id
  50. * @param $cate_id
  51. * @return bool
  52. * @throws \yii\db\Exception
  53. */
  54. public static function setData($coupon_id,$cate_id){
  55. try{
  56. if( $coupon_id&&$cate_id){
  57. $count = self::deleteAll([
  58. 'and',
  59. ['coupon_id' => $coupon_id],
  60. ['not in','cate_id',$cate_id]
  61. ]);
  62. $exist_cate_ids = self::find()->select('cate_id')
  63. ->where(['cate_id'=>$cate_id,'coupon_id' => $coupon_id])->column();
  64. $insert = [];
  65. $now = time();
  66. foreach($cate_id as $key => $id){
  67. if(in_array($id,$exist_cate_ids)) continue;
  68. $insert[] = [$coupon_id,$id,$now,$now];
  69. }
  70. $field = ['coupon_id','cate_id','created_at','updated_at'];
  71. // 批量插入数据
  72. Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
  73. }
  74. return true;
  75. }catch(Exception $e){
  76. self::$static_error = $e->getMessage();
  77. return false;
  78. }
  79. }
  80. }