Doctrine many to many left join

rokas

I have a problem with creating a query which should return cost centers not assigned to budget.

Database structure:

**Cost_center:**
+------+-----------+
|  id  |   title   |
+------+-----------+
| (PK) | (VARCHAR) |
+------+-----------+
\/
One 
to 
many
\/
**Budget_operation_scope_cost_center:**
+----------------+---------------------------+
| cost_center_id | budget_operation_scope_id |
+----------------+---------------------------+
| (FK)           | (FK)                      |
+----------------+---------------------------+
\/
Many 
to 
one
\/
**Budget_operation_scope:**
+------+-----------+-----------+
|  id  |   title   | budget_id |
+------+-----------+-----------+
| (PK) | (VARCHAR) | (FK)      |
+------+-----------+-----------+
\/
Many 
to 
one
\/
**Budget:**
+------+-------+
|  id  | year  |
+------+-------+
| (PK) | (INT) |
+------+-------+

Managed to do a query which returns assigned to budget cost centers list:

$query = $this->getEntityManager()
    ->createQueryBuilder()
    ->select('costCenter')
    ->from('ResourcesBundle:CostCenter', 'costCenter')
    ->leftJoin('costCenter.budgetOperationScope', 'budgetOperationScope')
    ->where('budgetOperationScope.budgetId = :budget')
    ->setParameter('budget', $budget)
    ->getQuery()->getResult();

Question: how to get cost centers, which are not assigned to budget?

Richard

This line is constraining your query too early and effectively making your left join a join:

->where('budgetOperationScope.budgetId = :budget')

You can move it into your left join like so:

->leftJoin('costCenter.budgetOperationScope', 'budgetOperationScope', 'WITH' 'budgetOperationScope.budgetId = :budget')

This way you will now get null rows for budgetOperationScope when a cost center has no budget.

So you can effectively do:

->where('budgetOperationScope IS NULL')

All together:

$query = $this->getEntityManager()
    ->createQueryBuilder()
    ->select('costCenter')
    ->from('ResourcesBundle:CostCenter', 'costCenter')
    ->leftJoin('costCenter.budgetOperationScope', 'budgetOperationScope', 'WITH' 'budgetOperationScope.budgetId = :budget')
    ->where('budgetOperationScope IS NULL')
    ->setParameter('budget', $budget)
    ->getQuery()->getResult();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Doctrine many-to-many join table not populating

From Dev

JPQL left outer join on many to many relationship

From Dev

mySQL many-to-many LEFT JOIN syntax

From Dev

SQL - One to Many join with left outer join

From Dev

Many to Many doctrine symfony

From Dev

SQLAlchemy many-to-many LEFT OUTER JOIN with sorted list of entities

From Dev

Mysql Left Join (1 to many relationship)

From Dev

Doctrine query WHERE IN - many to many

From Dev

Doctrine: Should this be a Many-to-Many?

From Dev

Doctrine many to many query builder

From Dev

Symfony Doctrine Many to Many insert

From Dev

EF LEFT OUTER JOIN instead of INNER JOIN in one to many relationships

From Dev

How does inner join work on a many-to-many relationship using Doctrine and Symfony2

From Dev

How to join Many-To-Many table with extra parameter and extra condition with doctrine QueryBuilder

From Dev

Doctrine One to Many relationship

From Dev

How to ORDER BY/MAX before GROUP BY after a LEFT JOIN to a many table?

From Dev

One-to-Many Left Join / Merge in Data.Table in R

From Dev

SQL left join only returns one row instead of many

From Dev

mysql select concat of one-to-many matches on left join

From Dev

SQL left join only returns one row instead of many

From Dev

SQL Query with Aggregate function on Left Join of One-to-Many Relationship

From Dev

Many to Many relation with join table

From Dev

Many to many join Php/mysql

From Dev

How to join many to many in createQuery()

From Dev

MongoDB Doctrine: $in needs an array - Many to Many Relation

From Dev

Many to many relation with ON DELETE CASCADE with Symfony and Doctrine

From Dev

Symfony Doctrine Query For Many to Many Releationships

From Dev

Doctrine ObjectMultiCheckBox form many-to-many grouped

From Dev

Doctrine - Criteria - expressions - contains (many to many)

Related Related

  1. 1

    Doctrine many-to-many join table not populating

  2. 2

    JPQL left outer join on many to many relationship

  3. 3

    mySQL many-to-many LEFT JOIN syntax

  4. 4

    SQL - One to Many join with left outer join

  5. 5

    Many to Many doctrine symfony

  6. 6

    SQLAlchemy many-to-many LEFT OUTER JOIN with sorted list of entities

  7. 7

    Mysql Left Join (1 to many relationship)

  8. 8

    Doctrine query WHERE IN - many to many

  9. 9

    Doctrine: Should this be a Many-to-Many?

  10. 10

    Doctrine many to many query builder

  11. 11

    Symfony Doctrine Many to Many insert

  12. 12

    EF LEFT OUTER JOIN instead of INNER JOIN in one to many relationships

  13. 13

    How does inner join work on a many-to-many relationship using Doctrine and Symfony2

  14. 14

    How to join Many-To-Many table with extra parameter and extra condition with doctrine QueryBuilder

  15. 15

    Doctrine One to Many relationship

  16. 16

    How to ORDER BY/MAX before GROUP BY after a LEFT JOIN to a many table?

  17. 17

    One-to-Many Left Join / Merge in Data.Table in R

  18. 18

    SQL left join only returns one row instead of many

  19. 19

    mysql select concat of one-to-many matches on left join

  20. 20

    SQL left join only returns one row instead of many

  21. 21

    SQL Query with Aggregate function on Left Join of One-to-Many Relationship

  22. 22

    Many to Many relation with join table

  23. 23

    Many to many join Php/mysql

  24. 24

    How to join many to many in createQuery()

  25. 25

    MongoDB Doctrine: $in needs an array - Many to Many Relation

  26. 26

    Many to many relation with ON DELETE CASCADE with Symfony and Doctrine

  27. 27

    Symfony Doctrine Query For Many to Many Releationships

  28. 28

    Doctrine ObjectMultiCheckBox form many-to-many grouped

  29. 29

    Doctrine - Criteria - expressions - contains (many to many)

HotTag

Archive