Answer the question
In order to leave comments, you need to log in
Doctrine DQL: how to select objects from two tables with different conditions?
There are two entity classes, I want to select objects from their tables with one query:
SELECT d, e
FROM OrganizerBundle:DailyTask d, OrganizerBundle:Event e
WHERE d.weekday = 'all' OR DATE_FORMAT(e.date,'%Y-%m-%d') = :date
ELECT d0_.weekday AS weekday_0, d0_.time AS time_1, d0_.length AS length_2, d0_.id AS id_3, d0_.name AS name_4, d0_.description AS description_5, e1_.date AS date_6, e1_.length AS length_7, e1_.id AS id_8, e1_.name AS name_9, e1_.description AS description_10 FROM daily d0_, events e1_ WHERE (d0_.weekday = 'all') OR (DATE_FORMAT(e1_.date,'%Y-%m-%d') = '2017-07-08')
Answer the question
In order to leave comments, you need to log in
If you need a result - then two requests. If you have tonsils through your ass, then docs.doctrine-project.org/en/latest/reference/nati...
The problem is that the WHERE clause only works for the first table, while all elements are selected from the second.
How to register so that the conditions are separate for two tables? UNION not suggested - Doctrine does not support.
And now what if it supports? Doctrine - does not solve this problem, since it is just a regular query builder. The problem is solved here by SQL. In such cases, you can use RAW query, discarding perfectionism:
$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();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question