QueryBuilder with manual join on two entities return mixed lines. #5801

Open
opened 2026-01-22 15:18:20 +01:00 by admin · 3 comments
Owner

Originally created by @Kytrix on GitHub (Dec 12, 2017).

Originally assigned to: @Kytrix on GitHub.

Hello,
I'm using:
doctrine/common v2.7.3

I have 2 entities, Dispatch and PackagingContent, both are virtually linked with a field name product_id. There is no relation in entity between them.
Inside my repository, I made a manual join:

$query =$this->createQueryBuilder('d')
->leftJoin('SIFrontendBundle:packagingContent', 'pc1' ,'WITH', 'd.product = pc1.product')
->addSelect('pc1')
->where('d.extractionid = :dispatchId')->setParameter('dispatchId', $dispatchId);
return $query->getQuery()->getResult();

Inside my results, instead of getting 50 lines of results, I get 100 lines with:

array(
[0] => SI\FrontendBundle\Entity\Dispatch
[1] => SI\FrontendBundle\Entity\PackagingContent
[2] => SI\FrontendBundle\Entity\Dispatch
[3] => SI\FrontendBundle\Entity\PackagingContent
....
)

so 50 lines of each entities mixed !

it's strange, there is a way to get 50 lines with an array or collection with the two enities inside? like this:

array(
[0] => array(
[0] =>SI\FrontendBundle\Entity\Dispatch
[1] => SI\FrontendBundle\Entity\PackagingContent
)
[1] => array(
[0] =>SI\FrontendBundle\Entity\Dispatch
[1] => SI\FrontendBundle\Entity\PackagingContent
)
....
)

Originally created by @Kytrix on GitHub (Dec 12, 2017). Originally assigned to: @Kytrix on GitHub. Hello, I'm using: doctrine/common v2.7.3 I have 2 entities, Dispatch and PackagingContent, both are virtually linked with a field name product_id. There is no relation in entity between them. Inside my repository, I made a manual join: > $query =$this->createQueryBuilder('d') > ->leftJoin('SIFrontendBundle:packagingContent', 'pc1' ,'WITH', 'd.product = pc1.product') > ->addSelect('pc1') > ->where('d.extractionid = :dispatchId')->setParameter('dispatchId', $dispatchId); > return $query->getQuery()->getResult(); Inside my results, instead of getting 50 lines of results, I get 100 lines with: > array( > [0] => SI\FrontendBundle\Entity\Dispatch > [1] => SI\FrontendBundle\Entity\PackagingContent > [2] => SI\FrontendBundle\Entity\Dispatch > [3] => SI\FrontendBundle\Entity\PackagingContent .... > ) so 50 lines of each entities mixed ! it's strange, there is a way to get 50 lines with an array or collection with the two enities inside? like this: > array( > [0] => array( > [0] =>SI\FrontendBundle\Entity\Dispatch > [1] => SI\FrontendBundle\Entity\PackagingContent > ) > [1] => array( > [0] =>SI\FrontendBundle\Entity\Dispatch > [1] => SI\FrontendBundle\Entity\PackagingContent > ) > .... > )
admin added the BugMissing Tests labels 2026-01-22 15:18:20 +01:00
Author
Owner

@lcobucci commented on GitHub (Dec 18, 2017):

@Kytrix it's kind of hard to understand what you're facing without the map of your entities... could you please send us a failing test case that reproduces that behaviour? It would help us a lot to identify and fix the issue you're describing.

You can find examples on 388afb46d0/tests/Doctrine/Tests/ORM/Functional/Ticket

@lcobucci commented on GitHub (Dec 18, 2017): @Kytrix it's kind of hard to understand what you're facing without the map of your entities... could you please send us a failing test case that reproduces that behaviour? It would help us a lot to identify and fix the issue you're describing. You can find examples on https://github.com/doctrine/doctrine2/tree/388afb46d0cb3ed0c51332e8df0de9e942c2690b/tests/Doctrine/Tests/ORM/Functional/Ticket
Author
Owner

@malukenho commented on GitHub (Feb 20, 2018):

@Kytrix it seems like doctrine doesn't support it. But you can fix it by using a DTO.

// DTO to hold the entities
namespace Foo\Bar;

class MyDto
{
    /** @var Entity1 */
    public $entity1;

    /** @var Entity2 */
    public $entity2;

    public function __construct(Entity1 $entity1, Entity2 $entity2)
    {
        $this->entity1 = $entity1;
        $this->entity2 = $entity2;
    }
}

Then on your query builder you configure the select as:

$query =$this->createQueryBuilder('d')
    ->select('NEW Foo\Bar\MyDto(d, pc1)')
   // ... more code ..
@malukenho commented on GitHub (Feb 20, 2018): @Kytrix it seems like doctrine doesn't support it. But you can fix it by using a `DTO`. ```php // DTO to hold the entities namespace Foo\Bar; class MyDto { /** @var Entity1 */ public $entity1; /** @var Entity2 */ public $entity2; public function __construct(Entity1 $entity1, Entity2 $entity2) { $this->entity1 = $entity1; $this->entity2 = $entity2; } } ``` Then on your query builder you configure the `select` as: ```php $query =$this->createQueryBuilder('d') ->select('NEW Foo\Bar\MyDto(d, pc1)') // ... more code .. ```
Author
Owner

@zeromodule commented on GitHub (Apr 16, 2024):

@malukenho it doesn't work with entities. Only with scalar values.

@zeromodule commented on GitHub (Apr 16, 2024): @malukenho it doesn't work with entities. Only with scalar values.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5801