Skip to content

How to COALCASE relation? #7395

@BonBonSlick

Description

@BonBonSlick
Q A
Version "doctrine/orm": "^2.5.4",

Support Question

Example, User entity has 3 fields, A,B,C;All are relations, 2 always null, 1 is always id, let it be type of user details object.

How to select all users and get it is details from 1 of the relations? Because it is 3 different tables to join and it depends on which field is not null. Eg

 return $this->repository->createQueryBuilder('u')
            ->select('u.id, 
              u.email, 
             COALESCE(u.a, u.b, u.c) as relationNotNull, ')
            ->join('u.relationNotNull', 'userTypeTableAlias')
            ->getQuery()
            ->getArrayResult();

it said
User has no association named relationNotNull
Ok, lets try this

        return $this->repository->createQueryBuilder('u')
            ->select('u.id, u.a, u.b, u.c')
            ->join('u.userType', 'userType')
            ->join('COALESCE(u.a, u.b, u.c) as userTypeRelation', 'userTypeTableAlias')
            ->getQuery()
            ->getArrayResult();

we have
[Syntax Error] line 0, col 403: Error: Expected Doctrine\ORM\Query\Lexer::T_ALIASED_NAME, got 'COALESCE'

How to do it right way?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions