How to write this SQL query in Doctrine 2.0 (and fetch results)?
(SELECT 'group' AS type,
CONCAT(u.firstname, " ", u.surname) as fullname,
g.name AS subject,
user_id,
who_id,
group_id AS subject_id,
created
FROM group_notification
JOIN users u ON(who_id = u.id)
JOIN groups g ON(group_id = g.id)
)
UNION
(SELECT 'event' AS type,
CONCAT(u.firstname, " ", u.surname) as fullname,
e.name AS subject,
user_id,
who_id,
event_id AS subject_id,
created
FROM event_notification
JOIN users u ON(who_id = u.id)
JOIN events e ON(event_id = e.id)
)
ORDER BY created
Syntax for declaring a union is same as that of declaring a structure except the keyword struct. Note : Size of the union is the the size of its largest field because sufficient number of bytes must be reserved to store the largest sized field. To access the fields of a union, use dot(.)
What Is UNION in SQL? The UNION operator is used to combine the data from the result of two or more SELECT command queries into a single distinct result set. This operator removes any duplicates present in the results being combined.
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
UNION ALL views can be used to improve load performance where it is too expensive to maintain secondary indexes for all rows in a table. Sybase IQ lets you split the data into several separate base tables (for example, by date). You load data into these smaller tables.
Well, I found maybe the best solution:
/**
* @Entity
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"group" = "NotificationGroup", "event" = "NotificationEvent"})
*/
class Notification {
// ...
}
And then two classes (NotificationGroup and NotificationEvent) extending Notification:
/**
* @Entity
*/
class NotificationGroup extends Notification {
//...
}
/**
* @Entity
*/
class NotificationEvent extends Notification {
//...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With