1
I am trying to understand how best to make a notification scheme for multiple actions and nodes. Basically one should notify someone whenever an action of the type message or enjoy is performed.
Message: Fulano de tal enviou uma mensagem
Enjoyed: Fulano de tal curtiu seu texto [prévia com texto]
The problem is to relate all this, because an accurate case of the user name and in another case relate to recover the text and link.
select
case tipo
when 'mensagem' then ( [...] )
when 'curtiu' then ( select concat( nome , '//' , acao , '//' , body ) from tabela
inner join [...] where [...] )
end `data`
from notificacao
As it is line-line, I thought to make a simple select with subquery in each case depending on the type of notification, use concat
and explode
to break the elements. The above query would return lines as Papa Cahrlie//curtiu//Lorem ipsum...
.
In this scheme each type would have a subquery, but I have more 'events' besides the example, and I will have more complex querys. I could even resort to views
if applicable.
TABLE:
CREATE TABLE `notificacao` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`USUARIO` int(11) DEFAULT NULL,
`CONTEUDO` int(11) DEFAULT NULL,
`TIPO` enum( '...' ),
`STATUS` enum( '0' , '1' ) DEFAULT '0',
)
My table is simple - generic - that records the user, the type of event and the reference of the content that suffered the action. Wanted opinions on working this select more simply and/ or efficiently.
Thank you, but as I said in the question, for every action there is a different relationship. 'Likes' relates to text to display a preview, and messages only need the author. Maybe the use of
UNION ALL
can replace theCASE
, but it doesn’t solve relationships.– Papa Charlie
I didn’t understand the problem of relationships; the idea of separating notifications into tables is exactly to allow JOIN from messages notifications with the message tables and JOIN from likes notifications with the likes tables. What you have in mind that it would be impossible to do the scheme this way?
– user25930
Message only needs to notify that X sent message to Y; Likes relates to X that Y likes text Lorem ipsum...;
UNION ALL
will not work because they are relationships for different fields.– Papa Charlie
If the notifications are too heterogeneous to inhabit a single table, you have to make two queries: one for the likes, another for the messages - the solution with CONCAT/EXPLODE only works until someone puts in your database some string with the delimiter you chose (accidentally or maliciously), and then your PHP will break all the wrong fields... You can implement an Escaping system, but then every time you put or take something from the database you will need to do and undo Escaping.
– user25930
Yeah, it’s crossed my mind this inconvenience. Making 2 separate queries will be a problem sort by date.
– Papa Charlie
I edited my reply for it to become a solution with LEFT OUTER Joins - you’ll get a clogged table of Nulls, but you’ll be able to have separate columns for separate notifications, and sort by date.
– user25930