Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kfirba's avatar
Level 50

Counting coupon usage

Hello.

In my application I have 3 entities:

  • User
  • Tag
  • Coupon

A user may "subscribe" to many tags and a coupon may have many tags. The user receives coupons based on his tag subscription. The tables to do so are:

  • users
  • tags
  • tag_user
  • coupon_tag

Everything works just fine. Now I want to count how many times a coupon was viewed but also limit it per user. So if user John accesses coupon #105 4 times, the coupon will show that it was consumed only once (as there was only 1 user viewing it).

Currently I have in my coupons table a consumed property which is an unsigned integer.

Do you think the only way I can limit the consumption of the coupon to be unique per user is to add another table consumed_coupons with user_id and coupon_id? Now I can remove the consumed property on the coupons table and when I need the count for the coupon I can just run a count() query based on the coupon_id.

0 likes
1 reply
rwdevguy's avatar

I would definitely do with the many_to_many option as I'm not sure how you would store it for each user other wise.

This will also bring about much more flexibility and you could easily extend your reporting if you needed to.

Please or to participate in this conversation.