How to write UNION in Doctrine 2.0

43,391

Solution 1

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 {
    //...
}

Solution 2

UNION is not supported in DQL, but you can still write a UNION query and use the Native Query capabilities to retrieve the data:

http://doctrine-orm.readthedocs.org/en/latest/reference/native-sql.html

However from your example it seems you want to use some form of table per class inheritance, which is not yet supported. There is another form of inheritance, (Joined Table Inheritance) that works though, if you can change your schema.

http://www.doctrine-project.org/projects/orm/2.0/docs/reference/inheritance-mapping/en#class-table-inheritance

A view would be another good solution, but then it depends on your database vendor if it also supports write operations or not.

Solution 3

$connection = $em->getConnection();
$query = $connection->prepare("SELECT field1, field2 FROM table1 
                                UNION
                                SELECT field3, field4 FROM table2 
                                UNION 
                                SELECT field5, field6 FROM table3
                                ");
$query->execute();
$result = $query->fetchAll();

Solution 4

UNION is not supported in Doctrine, s. the discussion here.

Share:
43,391

Related videos on Youtube

Iškuda
Author by

Iškuda

Updated on March 19, 2020

Comments

  • Iškuda
    Iškuda about 4 years

    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
    
  • Haim Evgi
    Haim Evgi over 13 years
    As an interm solution you could use a VIEW to get UNION support.
  • ihsan
    ihsan over 9 years
    And how is the explanation to this solution?
  • Radu C
    Radu C about 8 years
    @ihsan you select from the base class repository (Notification) and you get all the objects of that are of subclass type (NotificationGroup, NotificationEvent, and even just Notification if you haven't declared it abstract). If you need only certain types, then you can use the INSTANCE OF operator in WHERE.
  • Daniel Böttner
    Daniel Böttner about 4 years
    Would this approach in fact create a UNION SELECT query?
  • Jekis
    Jekis almost 4 years
    @Daniel Böttner, no. It will use table joins. doctrine-project.org/projects/doctrine-orm/en/2.7/reference/‌​…
  • Allart
    Allart over 2 years
    @FrancescoCasula also broken :)

Related