How to write UNION in Doctrine 2.0

后端 未结 4 915
清歌不尽
清歌不尽 2020-12-15 06:49

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.na         


        
相关标签:
4条回答
  • 2020-12-15 07:08

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

    0 讨论(0)
  • 2020-12-15 07:10

    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.

    0 讨论(0)
  • 2020-12-15 07:14
    $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();
    
    0 讨论(0)
  • 2020-12-15 07:22

    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 {
        //...
    }
    
    0 讨论(0)
提交回复
热议问题