Is it possible to use Doctrine to automatically build a result array that has a child DTO as a property?
For example, let's assume it is only possible to have a single child, and I have the following Entities:
#[Entity]
#[Table(name: 'person')]
readonly class PersonEntity
{
public function __construct(
#[Id]
#[GeneratedValue]
#[Column(type: Types::INTEGER)]
public ?int $id = null,
#[Column(type: Types::STRING, length: 255)]
public ?string $slug = null,
#[Column(type: Types::STRING, length: 255)]
public ?string $firstName = null,
#[Column(type: Types::STRING, length: 255)]
public ?string $lastName = null,
#[Column(type: Types::INTEGER)]
#[OneToOne(targetEntity: ChildEntity::class)]
public ?int $childId = null,
)
{
}
}
#[Entity]
#[Table(name: 'child')]
readonly class ChildEntity
{
public function __construct(
#[Id]
#[GeneratedValue]
#[Column(type: Types::INTEGER)]
public ?int $id = null,
#[Column(type: Types::STRING, length: 255)]
public ?string $slug = null,
#[Column(type: Types::STRING, length: 255)]
public ?string $firstName = null,
#[Column(type: Types::STRING, length: 255)]
public ?string $lastName = null,
)
{
}
}
Now I want to create a query that fetches all the people, and automatically populates a child property if they have a child set:
$select = 'NEW Person(
a.id,
a.slug,
a.firstName,
a.lastName,
case when c.id IS NULL then NULL else NEW Child(c.id, c.slug, c.firstName, c.lastName)
)';
$query = $this->entityManager->createQueryBuilder()
->select($select)
->from(PersonEntity::class, 'a')
->leftJoin(ChildEntity::class, 'c', Join::WITH, 'a.childId = c.id')
->getQuery();
Currently this is throwing the following error: [Syntax Error] line 0, col 150: Error: Unexpected 'NULL'
Is there a way to build these results automatically using Doctrine? Or do I have to manually hydrate and build the object structure using a list of array result data?
You can use the MakerBundle and let it generate the Entities for you. That way it's trivial to create relations. That way you don't need to write your own query. See the docs here: How to Work with Doctrine Associations / Relations.