Right join query in symfony3 with doctrine

AllexOne

I want to convert the sql query below to doctrine query in symfony.

select p.name,p.id,sum(t.amount) as bal from transactions t right join processors p on t.processor_id=p.id where user_id=18 or user_id is null group by p.id

The above code fetches balance from transactions table by summing up amounts of each transaction for a user for each processor.

Result:

Processor1 --------- 43

Processor2 --------- 12

Processor3 --------- NULL

Processor4 --------- NULL

Processor5 --------- NULL

The query i tried with dql is:

$sql = $procRepo->createQueryBuilder('t');
        $sql->select('p.name');
        $sql->leftJoin('t.processorId','p');
        $sql->addSelect('sum(t.amount) as bal');
        $sql->groupBy('p.id');
        $sql->orderBy('p.name')->getQuery()->getResult();

Result:

Processor1 --------- 43

Processor2 --------- 12

So my problem is i also want to get the NULL rows.

Note: I am using Symfony 3

Can anybody help?

yceruto

You needs to invert the join statement to gets all processors:

$sql = $procRepo->createQueryBuilder('p');
$sql->select('p.name', 'sum(t.amount) as bal');
$sql->leftJoin('p.transaction', 't');
$sql->groupBy('p.id');
$result = $sql->orderBy('p.name')->getQuery()->getResult();

This query must be made in your ProcessorRepository.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Right join query in symfony3 with doctrine

From Dev

Symfony Doctrine Join and In query

From Dev

Symfony3 - Using LEFT or RIGHT with doctrine

From Dev

Symfony2 Doctrine Query with Left Join and Objects as the result

From Dev

Right Join / Group by query

From Dev

to select the join with doctrine in symfony

From Dev

outer join in a query builder with doctrine

From Dev

Doctrine 2 Self Join Query

From Dev

Doctrine 2 Self Join Query

From Dev

Symfony Query doctrine entity

From Dev

Relation ManyToMany with doctrine/symfony3

From Dev

MySQL right outer join query

From Dev

Symfony 2 doctrine left join

From Dev

Symfony 2 Doctrine LEFT JOIN

From Dev

Symfony 2 doctrine left join

From Java

Right Join and Inner join in the same access query

From Dev

Doctrine returning join query in different arrays

From Dev

Multiple join query builder doctrine2

From Dev

Convert SQL inner join query to Doctrine

From Dev

Symfony2 - How to query a left-join with a condition in doctrine 2

From Dev

PostgreSQL Query with aggregates and RIGHT JOIN not filtering

From Dev

Rewrite query without right join in jpa

From Dev

Sub query filter in LEFT/RIGHT JOIN

From Dev

Right outer join in linq to entities query

From Dev

PHP RIGHT JOIN query not considering null?

From Dev

update createdAt & updatedAt fields automatically in symfony3 (doctrine)

From Dev

Symfony3 Doctrine findByDate filter by month and/or year

From Dev

Symfony3, Doctrine2, custom fixtures loader

From Dev

Symfony3 Doctrine2 can not access variable in the loop