简体   繁体   中英

In CakePHP, how do I efficiently make sure all users have acknowledged system messages?

The idea: From time to time, I will be adding a new "system wide" message into a table. What I want is to make sure that every user continues to see this message at the top of their screen, until they acknowledge it.

Sometimes, a second message might come out before they acknowledge the first, in which case I'd like to make sure they acknowledge both.

Implementation: Presently, my idea was to have a field in the user table, tracking the last acknowledged message id. My hesitation there is that every single time a page is loaded, there would have to be a comparison of that user's last message id and the most recent message from the database. That seems like a large tax on my system for a relatively small feature.

How do I go about this concept efficiently?

Can they acknowledge message #2 without acknowledging message #1? If so, you'll need a different design, so you can track the status of messages individually.

If not, what I would do is have an "acknowledged" field that is either timestamp or datetime in the users table. When the user hits "acknowlege" you update the "acknowledged" field to the current time. You then also store the datetime/timestamp of when messages are created. Then whatever operations you need to do on messages and users should be pretty straightforward.

I really would go with the solution suggested by Kai , but here are 2 other ways of doing it, which could be reused for future use-cases.

Save a list of unread messages

When creating a new message, create a record for each current user in an associated model. When fetching the user and saving it in the session your view can check if the field is empty or not. This has the side-effect that users created after that message will not see this message.

Save a list of all messages

Even another (and cleaner approach) without above side-effect: Save the read messages in an associated model. When finding a user, create a virtual field or use afterFind to create the inverse list of read messages. Make sure you cache the list of all existing messages for this step.

That way you can also save when a user acknowledged your message (if that could be helpful).


In all cases you can rely on the Auth-Data which is provided anyway. Complexity does not really matter that much in this case and would be O(1).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM