Any ideas about email subscription?

I am wondering how to generate a email subscription button just like this forum’s watch this topic button…

How to implement this?

Same as the FEED?

…Any hints?

Thanks in advance

any ideas…?

Displaying the subscription form to registered users should be no problem. Write the subscription somewhere in your DB.

Then just create a CLI command in your Yii application that will read the DB and send emails. Add it to your cron.

The DB seems obvious : a simple relation table with fields "user_id", "topic_id", "subscription_type", "last_read".

Thanks for your help!

So a database with fields

id

user_id +FK

topic_id +FK

subscription_type

last_read

would be fine right?

What is the last_read column for ?

Is it necessary?

for example, what if i just notify the user after a post is made?

Thx!

Yes, I think it should be fine.

“id” isn’t strictly necessary, you could use a composite PK (user_id, topic_id). But a simple autoinc int PK will be easier to handle through Yii’s ActiveRecord models.

Your cron script will need to find all the subscriptions that will receive a message. For this, you need to match recent posts (Post.last_update) against the date of the last time you looked for news (Subscription.last_read).

You could find all the new posts with a SQL like:




SELECT *

FROM Subscription as s

 JOIN Post AS p USING (topic_id)

WHERE p.last_update > s.last_read

ORDER BY s.id, topic_id, last_update



This column could be replaced with a global “last_cron” timestamp value. But this would make it harder to handle multiple periods of subscriptions (e.g. daily and weekly). If this column isn’t there would also be no way to deal with incomplete runs: if your cron stops for whatever reason, then running it again will lead to duplicate or missing messages.

Thank you so much. That helps a lot!!!

Best,

jiaming